Get selected items in checkboxlist

iterate checkboxlist

When using checkbox list control in we need to find multiple selected items from Checkbox list. For example there is a Checkbox list with subject list and we need to show the items those are selected.

<html xmlns=""> 
<head id="Head1" runat="server"> 
<form id="form1" runat="server"> 
<asp:CheckBoxList ID="CheckBoxList1" runat="server"> 
<asp:ListItem Value="0">ASP.NET</asp:ListItem> 
<asp:ListItem Value="1">C#.NET</asp:ListItem> 
<asp:ListItem Value="2">VB.NET</asp:ListItem> 
<asp:ListItem Value="3">WCF</asp:ListItem> 
<br /> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show Selected" /> 
<br /> 
<br /> 
<asp:Label ID="lblSelected" runat="server" Text=""></asp:Label> 

Code that shows selected item from checkbox list [C#]

using System.Web.UI.WebControls;

namespace aspnetcontrol
    public partial class CheckboxList : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        protected void Button1_Click(object sender, EventArgs e)
            lblSelected.Text = "";

            foreach (ListItem item in CheckBoxList1.Items)
                if (item.Selected == true)
                    lblSelected.Text += "Selected Subject/s: " + item.Text + "



Post a Comment