I have a CheckBoxList on my asp.net web page with the list items hard coded into the HTML (so no postback / data binding issues at play). When someone clicks the submit button, I iterate through the items in the CheckBoxList and create a comma delimited string of any checked values. The problem, however, is that no matter what, the listitem's .Selected property is always false. All the solutions I've found have to state to check !Page.IsPostBack to make sure the data isn't being wiped but a) I've already checked that and b) it's not really an issue since I'm not dynamically loading the list control. Below is my code:
Markup
<asp:CheckBoxList id="chkGenderList" runat="server" data-paramname="genders">
<asp:ListItem Value="M" Text="Male" />
<asp:ListItem Value="F" Text="Female" />
<asp:ListItem Value="" Text="Unknown" />
</asp:CheckBoxList>
Code Behind
private string GetCheckBoxListValues(CheckBoxList chkList)
{
StringBuilder sb = new StringBuilder ();
foreach (ListItem lst in chkList.Items) {
if (lst.Selected)
sb.Append (lst.Value).Append (",");
}
if(sb.Length > 0)
sb.Remove (sb.Length - 1, 1);
return sb.ToString ();
}
I can step through and iterate through the list. I certainly have items in the checkboxlist I am passing into the method, but every single ListItem, .Selected is false. Any ideas?
