Slide 11.6: Creating a dynamic checkbox list
Slide 11.8: Creating a dynamic checkbox list (cont.)
Home

Creating a Dynamic Checkbox List (Cont.)


This example contains one page, WebForm1.aspx, which includes the following tools: Note that the CheckBoxList is different from CheckBox, which has no DataSourceID property, so it is not able to receive data from a database. The following is a simplified source code of the WebForm1.aspx:

(Simplified) WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true"
  CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <body>
  <form id="form1" runat="server">

   Dynamic Checkboxes
   <asp:Label ID="Label2" runat="server" Text="Course title:"></asp:Label>
   <asp:TextBox ID="title" runat="server" Width="203px"></asp:TextBox>
   <asp:Button ID="Add" runat="server" OnClick="Add_Click" Text="Add" />
   <asp:Label ID="Label3" runat="server" Text="Student name:"></asp:Label>
   <asp:TextBox ID="studentName" runat="server" Width="159px"></asp:TextBox>
   <asp:Label ID="Label4" runat="server" Text="Course list:"></asp:Label>
   <asp:Button ID="Reset" runat="server" Text="Reset" />

   <asp:SqlDataSource ID="enrollment" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
    ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>"
    SelectCommand="SELECT [title] FROM [Courses] ORDER BY [title]">
   </asp:SqlDataSource>

   <asp:CheckBoxList ID="courseList" runat="server" DataSourceID="enrollment"
    DataTextField="title" DataValueField="title" AutoPostBack="True">
    <asp:ListItem></asp:ListItem>
   </asp:CheckBoxList>

   <asp:Label ID="Result" runat="server"></asp:Label>
  </form>
 </body>
</html>