CheckboxList not setting Selected with Viewstate

Posted by Earlz on Stack Overflow See other posts from Stack Overflow or by Earlz
Published on 2010-04-27T20:20:28Z Indexed on 2010/04/27 20:23 UTC
Read the original article Hit count: 390

Filed under:
|
|
|

I have a CheckboxList that seems to load and do everything right, except for when I do a postback, it will not have the Item.Selected property set. I have viewstate disabled for the entire page.

I load it like so(inside Page_Load on every load):

foreach (DataRow service in d.Tables[0].Rows)
{
  cblServices.Items.Add(new ListItem((string)service["description"], service["id"].ToString()));
}

My markup is simple:

<asp:CheckBoxList runat="server" ID="cblServices" Width="300px"></asp:CheckBoxList>

and then, I use basically something like this(in a _Click serverside event for a button)

foreach(ListItem item in cblServices.Items){
  if(item.Selected){
    MyLabel.Text+="selected: "+item.Value+item.Text;
  }
}

and MyLabel never has any text added to it. I can verify with the debugger that it does reach the _Click's foreach loop, but no item is ever selected. What could be the cause of this?

© Stack Overflow or respective owner

Related posts about checkboxlist

Related posts about postback