Slide 13.8: HTML server controls (cont.)
Slide 13.10: Web server controls
Home

Web Server Controls


Like HTML server controls, web server controls are also created on the server. However, web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements. The syntax for creating a web server control is
   <asp:control_name id="some_id" runat="server" />
The following example shows the use of CheckBoxList server control, which creates a multi selection check box group.


dem_checkboxlist.aspx

Web
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="C#" runat="server">
 protected void Check1_SelectedIndexChanged( object sender, EventArgs e ) {
  msg.Text = "<p>Selected Item(s):</p>";
  for ( int i = 0; i < Check1.Items.Count; i++ )
   if ( Check1.Items[i].Selected )
    msg.Text += Check1.Items[i].Text;
 }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server"><title></title></head>
 <body>
  <form id="form1" runat="server">
   <asp:CheckBoxList ID="Check1" runat="server" 
     AutoPostBack="True"
     onselectedindexchanged="Check1_SelectedIndexChanged">
    <asp:ListItem>Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
    <asp:ListItem>Item 4</asp:ListItem>
    <asp:ListItem>Item 5</asp:ListItem>
    <asp:ListItem>Item 6</asp:ListItem>
   </asp:CheckBoxList>
   <asp:Label ID="msg" runat="server"></asp:Label>
  </form>
 </body>
</html>