Geeks With Blogs
Thanigainathan Siranjeevi Sharing my learning

Known is a drop, Unknown is a world. I had a small requirement where we have to show a Checkboxlist with some collection. I thought it was a easy stuff as it wa s dealing with a normal Checkbox collection. The requirement was to show some set of options in the collection, in addition there will be a option showing as "Select All" . This was a requirement given by my friend. I thought helping out and found how difficult it was. You may think easy as I first thought. We will see about that.

Liks referred:

The task was to display a Checkbox collections like below.

For demo purpose we will be hard coding the value in the <asp:ListItem> tags. This will add some values to the checkbox for now.

<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Sel_Changed">

<asp:ListItem Text="SelectAll" Value="SelectAll"></asp:ListItem>

<asp:ListItem Text="Sample" Value="Sample"></asp:ListItem>

<asp:ListItem Text="Test" Value="Test"></asp:ListItem>

<asp:ListItem Text="Mic" Value="Mic"></asp:ListItem>

<asp:ListItem Text="Pen" Value="Pen"></asp:ListItem>

</asp:CheckBoxList>

 

The hardest part of the requirement comes now. I have to add one of the option like "SelectAll". The functionality that is expected from this option is explained in below points.

  1. When this is selected the user will be able to select all other options in the collection.
  2. When its unticked all will be deselected
  3. When all are selected and one of the options is unticked then the control will be deselected with the "Select ALL" option.
  4. When "SelectAll" options is not selected and all other options are selected then automatically the "Select All " option should be showing.

At first I thought it was easy. As I gradually started giving the solution I found the problem. The problem is the state of the control not being. This is a basic problem which took long time for me to identify. Because at each selected option one has to check with the previous selected option's for giving the solution . It took long time for me to find this.

Then did a little research and tried to implement the Session object. The solution is simple but powerful. It solves the problem. I am damn sure everyone who reads will get benefit because of this solution. Nowhere in the net could I find a possible solution first. The alternative will be the ViewState.

Just try to store the checkboxlist items property in the session during the first possible postback from the control. Then in the second postback do the mainpulations with the session object and current state of the object. I can give a small Algorithm.

  1. Check Session for Previous collections.
    1. If present then cast them for ListCollection instance.
    2. Get the count of select item for previous and current items
    3. If both count are not equal and "SelectAll" option for current object is true them we have to deselect "SelectAll" option.
    4. Else If previous "SelectAll" is false and current "SelectAll" is true then select all the current options.
    5. Else If previous "SelectAll" is true and current "SelectAll" is false then Deselect all the current options.
  2. If no previous collection available then do normal operation for selecting the items.
    1. Else check if the SelectAll option is deselected when all item is selected and if yes select them .
    2. save the current collection in session

The Code sample is given below. This is the event for SelectIndexChanged event of Checkboxlist control.

protected void Sel_Changed(object sender, EventArgs e)

{

int cntr = 0;

int prevcntr = 0;

 

//Get Current count

cntr = GetCount(this.CheckBoxList1.Items);

 

//Check Session

if (Session["obj"] != null)

{

ListItemCollection ls = (ListItemCollection)Session["obj"];

//Get previous state count

prevcntr = GetCount(ls);

 

//For deselcting the SelectAll when one of the other items is selected

if (cntr != prevcntr && CheckBoxList1.Items.FindByValue("SelectAll").Selected == true)

{

CheckBoxList1.Items.FindByValue("SelectAll").Selected = false;

}// For SelecteAll true option

else if (ls.FindByValue("SelectAll").Selected == false && CheckBoxList1.Items.FindByValue("SelectAll").Selected == true)

{

SelectAllOptions(true);

}// For SelecteAll False option

else if (ls.FindByValue("SelectAll").Selected == true && CheckBoxList1.Items.FindByValue("SelectAll").Selected == false)

{

SelectAllOptions(false);

}

Session.Add("obj", null);

}

else

{

//Normal SelectAll option

if (this.CheckBoxList1.Items.FindByValue("SelectAll").Selected == true)

{

SelectAllOptions(true);

}

else

{

//For Selecting the SelectAll option during the selection of all items

if (this.CheckBoxList1.Items.Count - cntr == 1 && CheckBoxList1.Items.FindByValue("SelectAll").Selected == false)

{

CheckBoxList1.Items.FindByValue("SelectAll").Selected = true;

}

}

Session.Add("obj", CheckBoxList1.Items);

}

}

 

void SelectAllOptions(Boolean val) {

foreach (ListItem lst in this.CheckBoxList1.Items)

{

lst.Selected = val;

}

}

 

int GetCount(ListItemCollection ls) {

int prevcntr=0;

foreach (ListItem lst in ls)

{

if (lst.Selected == true)

{

prevcntr = prevcntr + 1;

}

}

return prevcntr;

 

 

Yeah that's it and all has been done. You can possibly download the sample here.

Conclucion:

This is a very peculiar scenario one may face. This will be very helpful for you.

Thanks,

Thanigainatha Siranjeevi

Posted on Tuesday, June 9, 2009 1:31 PM | Back to top


Comments on this post: CheckboxList State Maintenance

# re: CheckboxList State Maintenance
Requesting Gravatar...
bery good u explained clearly.it Satisfys all the requirements.
Left by raveendranath. on Nov 14, 2009 12:38 AM

# re: CheckboxList State Maintenance
Requesting Gravatar...
Thank you so much..it was really very helpful.
Left by chandra on Jul 17, 2012 9:54 AM

Your comment:
 (will show your gravatar)


Copyright © thanigai | Powered by: GeeksWithBlogs.net