Hey all....i am stuck with a very small task...i have 3 items in my checkboxlist(asp.net,v csharp).
i want that if i select more than 1 items it shuld give me the count....as in 2 items selected...etc.
Plz......Help....!....i got 2 submit my assignment by 8 am tomorrow......thanx...
Loading
Niradhip ChakrabortyPosted Apr 9, 2008, 5:20 AM
Hi,
You can use the following code to count the total number of checkboxes in the checkboxlist have been checked.
protectedvoid Button1_Click(object sender, EventArgs e)
{
int numSelected = 0;
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
numSelected = numSelected + 1;
}
}
Response.Write("Total Number Of CheckBoxes Selected:");
Response.Write(numSelected);
}
Let me know if u have any doubts.
Rafiq BatchaPosted Apr 8, 2008, 7:35 PM
Hi,
Use the following code to solve this. You must use this property AutoPostBack="true". Otherwise postback will not happen.
<form id="form1" runat="server"> <div> <asp:CheckBoxList ID="cblTest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="cblTest_SelectedIndexChanged" > <asp:ListItem Text="aaa" Value="aaa">asp:ListItem> <asp:ListItem Text="bbb" Value="bbb">asp:ListItem> <asp:ListItem Text="ccc" Value="ccc">asp:ListItem> asp:CheckBoxList><br /> <asp:Label ID="lblAlertMsg" runat="server" ForeColor="red">asp:Label> div> form> protected void cblTest_SelectedIndexChanged(object sender, EventArgs e){
int selectedItemCnt = 0;lblAlertMsg.Text =
""; for (int i = 0; i < cblTest.Items.Count; i++){
if (cblTest.Items[i].Selected)selectedItemCnt += 1;
}
if (selectedItemCnt > 1)lblAlertMsg.Text =
"You have selected " + selectedItemCnt.ToString() + " items.";}