Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Web Controls

To deal with the large number of web control we will divide into the following categories.

Simple Controls

  1. Labels are the simplest control

  2. TextBox

Properties

Examples

// Before
<asp:TextBox ID="UserName" RunAt="server" />
<asp:TextBox ID="Password" TextMode="Password" RunAt="server" />
<asp:TextBox ID="Comments" TextMode="MultiLine" Rows="10"
  RunAt="server" />

// After
<input name="UserName" type="text" id="UserName" />
<input name="Password" type="password" id="Password" />
<textarea name="Comments" rows="10" id="Comments"></textarea>

Events

Since normally only the submit button will cause a post back, here is how this happens on the client (using Jscript on the client and DHTML)
Example

<asp:TextBox ID="UserName" AutoPostBack="true" RunAt="server" />

and the control outputs this:

<input name="UserName" type="text" id="UserName"
  onchange="__doPostBack('UserName','')" language="javascript" />
    .
    .
    .
<script language="javascript"> 
<!--
  function __doPostBack(eventTarget, eventArgument) {
      var theform = document.ctl0;
        .
        .
        .
      theform.submit();
  }
// -->
</script>

HyperLink Control

Adds a hyperlink to a web page (which can be changed under program control)

<asp:HyperLink Text="Click here"
  NavigateUrl="http://www.wintellect.com" RunAt="server" />
<asp:HyperLink ImageUrl="logo.jpg"
  NavigateUrl="http://www.wintellect.com" RunAt="server" />
<asp:HyperLink ID="MyLink" Text="Web page du jour" RunAt="server" />
  .
  .
  .
<script language="C#" runat="server">
  void Page_Load (Object sender, EventArgs e)
  {
      MyLink.NavigateUrl = "www.wintellect.com";
  }
</script>

Checkbox

<asp:CheckBox ID="Confirm" Text="E-mail my confirmation"
  RunAt="server" />
if (Confirm.Checked) {
    // The box is checked
}
else {
    // The box is not checked
}

-----------------

<asp:CheckBox ID="Confirm" Text="E-mail my confirmation"
  AutoPostBack="true" OnCheckedChanged="DoItNow" RunAt="server" />
    .
    .
    .
<script language="C#" runat="server">
  void DoItNow (Object sender, EventArgs e)
  {
      // The check box was just checked or unchecked; do something!
  }
</script>

RadioButton

asp:RadioButton Text="Red" ID="Button1" Checked="true"   GroupName="Colors" RunAt="server" /><br> 
 <asp:RadioButton Text="Green" ID="Button2"   GroupName="Colors" RunAt="server" /><br>
 <asp:RadioButton Text="Blue" ID="Button3"   GroupName="Colors" RunAt="server" /><br> <br>
 <asp:RadioButton Text="Circle" ID="Button4" Checked="true"   GroupName="Shape" RunAt="server" /><br>
 <asp:RadioButton Text="Square" ID="Button5"   GroupName="Shape" RunAt="server" />


Table Controls

Table controls add HTML tables to Web forms. They render a combination of <table>, <tr>, and <td> tags to browsers. Here’s one way to add a table to a Web form:

<table>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
  </tr>
</table>

And here’s the equivalent table created with a Table control:

<asp:Table ID="MyTable" RunAt="server">
  <asp:TableRow>
    <asp:TableCell>Row 1, Column 1</asp:TableCell>
    <asp:TableCell>Row 1, Column 2</asp:TableCell>
  </asp:TableRow>
  <asp:TableRow>
    <asp:TableCell>Row 2, Column 1</asp:TableCell>
    <asp:TableCell>Row 2, Column 2</asp:TableCell>
  </asp:TableRow>
</asp:Table>

Table controls add value to a Web form when you want to change a table’s contents dynamically. For example, the following server-side script modifies the text in each of the table cells:

MyTable.Rows[0].Cells[0].Text = "Cell 1";
MyTable.Rows[0].Cells[1].Text = "Cell 2";
MyTable.Rows[1].Cells[0].Text = "Cell 3";
MyTable.Rows[1].Cells[1].Text = "Cell 4";

This script builds the entire table at run time:

<asp:Table ID="MyTable" RunAt="server" />
  .
  .
  .
<script language="C#" runat="server">
  void Page_Load (Object sender, EventArgs e)
  {
      for (int i=0; i<2; i++) {
          TableRow row = new TableRow ();
          for (int j=0; j<2; j++) {
              TableCell cell = new TableCell ();
              cell.Text = String.Format ("Row {0}, Column {1}",
                  i + 1, j + 1);
              row.Cells.Add (cell);
          }
          MyTable.Rows.Add (row);
      }
  }
</script>

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.