Disabling CheckBoxes in a CheckBoxList in Asp.net 2.0

In live applications or our day to day real time scenario’s we come across such type of situations
where in a ListBox or CheckBoxList, we need to disable or deselect the remaining items when all is selected.

You might be wondering how to do it. But it's very simple...

In Checkbox there is a chance that nothing can be selected where in ListBox selection is Mandatory.
And also CheckBoxList is rendered as HTML table and individual items as Checkboxes . (Check in View -->source of your application.

1.Drag and Drop Asp.net CheckBoxList.
2.Make the RepeatDirection property as Horizontal.
3.Add some items like All, London, NewYork and Delhi

Now place the below code







protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (CheckBoxList1.Items[0].Selected)
{
for (int i = 1; i < CheckBoxList1.Items.Count; i++)
{
CheckBoxList1.Items[i].Enabled = false;
CheckBoxList1.Items[i].Selected = false;
}
}

else
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
CheckBoxList1.Items[i].Enabled = true;
}
}







Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: