Lecture – Form Objects – CheckBox List


 CheckBoxList Web Control

Prerequisites

Lecture – Getting Started in Visual Studio

Summary

Part 1 -The basics of how a CheckBoxList handles indexes

Part 2 – How to retrieve and set values in a CheckBoxList

Video 

Part 1 – http://online1.daytonastate.edu/player2.php?id=82ca5dd156cc926b2992f73c2896f761 

Part 2 – http://online1.daytonastate.edu/player2.php?id=55a988dfb00a914717b3000a3374694c 

Reference Materials

<body>    <form id=”form1″ runat=”server”>    <div>    <table><tr>    <td>        <asp:CheckBoxList ID=”cblMain” runat=”server”             onselectedindexchanged=”cblMain_SelectedIndexChanged” AutoPostBack=”True”>        <asp:ListItem>AUTO</asp:ListItem>        <asp:ListItem>MC</asp:ListItem>        <asp:ListItem>BICYCLE</asp:ListItem>        <asp:ListItem>FALL</asp:ListItem>        <asp:ListItem>GSW</asp:ListItem>        <asp:ListItem>HELMET</asp:ListItem>        <asp:ListItem>OTHER</asp:ListItem>        </asp:CheckBoxList>        </td>        <td >            <asp:CheckBoxList ID=”cblAuto” runat=”server” Visible=”false”>            <asp:ListItem>Driver</asp:ListItem>            <asp:ListItem>Passenger</asp:ListItem>            <asp:ListItem>Pedestrian</asp:ListItem>            <asp:ListItem>Airbag</asp:ListItem>            </asp:CheckBoxList>            <asp:CheckBoxList ID=”cblHelmet” runat=”server” Visible=”false”>            <asp:ListItem>None</asp:ListItem>            <asp:ListItem>1/2</asp:ListItem>            <asp:ListItem>3/4</asp:ListItem>            <asp:ListItem>Full</asp:ListItem>            </asp:CheckBoxList>        </td>        </tr>        </table>    </div>    <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>    <asp:Label ID=”Label2″ runat=”server” Text=”Label”></asp:Label>    </form></body>
 protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                string s = “AUTO;HELMET;GSW”;                string[] t = s.Split(new Char[] { ‘;’, ‘,’ });                foreach (string u in t)                {                    for (int i = 0; i < cblMain.Items.Count; i++)                        if (u.Trim() == cblMain.Items[i].Value) cblMain.Items[i].Selected = true;                }            }        }        protected void cblMain_SelectedIndexChanged(object sender, EventArgs e)        {            if (cblMain.Items[0].Selected) cblAuto.Visible = true; else cblAuto.Visible = false;            if (cblMain.Items[5].Selected) cblHelmet.Visible = true; else cblHelmet.Visible = false;            string s = String.Empty;            for (int j = 0;j<cblMain.Items.Count; j++)            {                if (cblMain.Items[j].Selected) s += cblMain.Items[j].Value + “;”;                            }            Label1.Text = s;        }

Additional Information

All about the CheckBoxList Control – http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx 

The String Split method demonstrated in the Video 2 – http://msdn.microsoft.com/en-us/library/y7h14879