Hi,I want to count the number of checkboxes which are checked on my asp.net page and if the count = 10 then to change the my label1 text to "Complete".
I understand that I have to use a for loop statement e.g for (int i; i<10; i++) and and an if statement to change the label text e.g if (i=10) {label.text="Complete"}. However I am not sure how to count a selection of checkboxes. Does anyone know how this can be achieved?
Loading
Gaurav GuptaPosted Apr 12, 2013, 4:45 AM
protected void checkBoxCategoryList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
int count = 0;
foreach (ListItem items in checkBoxCategoryList.Items)
{
if (items.Selected)
{
count++;
}
}
if (count > 10)
{
string result = Request.Form["__EVENTTARGET"];
string[] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
checkBoxCategoryList.Items[index].Selected = false;
ScriptManager.RegisterStartupScript(this, this.GetType(), "Hi", "alert('Category can not be select more than 10.');", true);
Or you can set the label value: (As you say in the question)..
Label.text="Complete"
}
catch { }
}