1
votes

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?

1
Why are you using a CheckBoxList? Do you really expect users to select multiple values here? Or is this just an example of what you're trying to do? - Mike U
When do you call this method? In which stage of page life cycle? - Andrei
When I test your code in the event handler of a button click, it works perfectly. - ConnorsFan
I ran your code. It works fine for me. The only different thing I did is I didn't check on Page.IsPostback. Instead I check on validity: if (Page.IsVAlid) {...}. - Kami
I'm using a checkboxlist because this is for a search form. So I legitimately need to provide the user with the ability to specify multiple values (as opposed to a single value in which case I'd use a radio button). - Shane McGarry

1 Answers

0
votes

If I have a blank .aspx page and I paste the code below into it,it works.

Try doing the same to solve your problem - start with a blank page and copy the code below into it, then slowly start adding any additional controls\functionality\ajax\javascript files until the code stops working, then you will know what caused your issue.

It's possible that there's a piece of javascript that is unchecking all the checkboxes or something similar.

.ASPX:

 <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>
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

Code behind:

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSave_Click(object sender, EventArgs e)
{
    string values = this.GetCheckBoxListValues(chkGenderList);
}

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();
}

Output:

CheckBoxList in ASP.NET web forms