Showing posts with label asp.net checkboxlist. Show all posts
Showing posts with label asp.net checkboxlist. Show all posts

Get selected items in checkboxlist asp.net

iterate checkboxlist

When using checkbox list control in asp.net 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="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<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> 
</asp:CheckBoxList> 
<br /> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show Selected" /> 
<br /> 
<br /> 
<asp:Label ID="lblSelected" runat="server" Text=""></asp:Label> 
</div> 
</form> 
</body> 
</html> 

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 + "
";
                }
            } 

        }
    }
}