I have a checkboxlist that i am populating from a SQL command and when the items are selected they are added into a cookie.
Im having a problem when trying to use the values in the cookie to select the right items when the page loads up. I got the bit of code below and it works fine until the part where i define the ListItem. This always comes up as null even though the values
can be read from the cookie and items with that name are present in the list.
Nevermind, i've just found what was wrong. I needed to databind the checkboxlist so that the values actually appeared. Seems like it couldn't see them that early on in the load
Marked as answer by a7Jymoz on Apr 03, 2012 12:57 PM
a7Jymoz
Member
2 Points
16 Posts
Select Items in checkboxlist from cookie
Apr 03, 2012 12:42 PM|LINK
Hi all
I have a checkboxlist that i am populating from a SQL command and when the items are selected they are added into a cookie.
Im having a problem when trying to use the values in the cookie to select the right items when the page loads up. I got the bit of code below and it works fine until the part where i define the ListItem. This always comes up as null even though the values can be read from the cookie and items with that name are present in the list.
Can anyone help?
HttpCookie cookie = Request.Cookies["CheckBoxList1_SelectedItems"]; if (cookie != null && cookie.Values != null) { foreach (string key in cookie.Values.Keys) { ListItem item = CheckBoxList1.Items.FindByValue(key); if (item != null) { item.Selected = true; } } }a7Jymoz
Member
2 Points
16 Posts
Re: Select Items in checkboxlist from cookie
Apr 03, 2012 12:57 PM|LINK
Nevermind, i've just found what was wrong. I needed to databind the checkboxlist so that the values actually appeared. Seems like it couldn't see them that early on in the load