↑ Grab this Headline Animator

Monday, November 24, 2008

Looping through CheckBoxList control.

For looping through any ListControl we generally use foreach loop element it to ListItem.
The foreach statement repeats a group of embedded statements for each element in an array or an object collection.
Here like GriviewItem for GridView or DataListItem for DataList we dont have CheckBoxList item for CheckBoxList object collection.
We need to use ListItem as element of CheckBoxList collection.

<h1>Select your favorite Sports</h1>

<asp:CheckBoxList ID="CheckBoxList1" runat="server">

    <asp:ListItem Text="Cricket" Value="1"></asp:ListItem>

    <asp:ListItem Text="Tennis" Value="2"></asp:ListItem>

    <asp:ListItem Text="FootBall" Value="3"></asp:ListItem>

    <asp:ListItem Text="Table Tennis" Value="4"></asp:ListItem>

    <asp:ListItem Text="Hockey" Value="5"></asp:ListItem>

    <asp:ListItem Text="Baseball" Value="6"></asp:ListItem>



string selectedText = string.Empty;

string selectedValue = string.Empty;

// looping ListView Control

foreach (ListItem li in CheckBoxList1.Items)


     if (li.Selected == true)


         selectedText = li.Text;

         selectedValue = li.Value;

         // Do your logic to handle selected items.

        // like insert into DB. or create comma separated list and use it.




Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles