0

I have a checkboxlist where certain items are disabled for certain users.

enter image description here

When I click on 'Save', the below code is executed.

 foreach (ListItem item in myCheckBoxList.Items)
 {
    if (!item.Selected)
            {
                continue;
            }
    selectedValues.Add(item.Value);
  }  

However, item.Selected evaluates to false for the disabled items even though they're selected.

Is there a way to get around this?

Pindub_Amateur
  • 328
  • 1
  • 6
  • 19
  • I think it is because disabled checkboxes are not really sent to server with POST. See [this answer](http://stackoverflow.com/questions/4727974/how-to-post-submit-an-input-checkbox-that-is-disabled) for more details – Paweł Hemperek Oct 10 '16 at 12:44

2 Answers2

3

Disabled inputs are never posted to the server, hence it will be set to default value, i.e. false. You can use HiddenField and associate that with each checkbox and set it's value based on it's selection.

Imad
  • 7,126
  • 12
  • 55
  • 112
  • This is incorrect. Have you even tested it? They are not posted to the server but that does not mean aspnet does not reconize them as checked. – VDWWD Oct 10 '16 at 14:28
  • WHAT??? They are not posted to the server but that does not mean aspnet does not reconize them as checked??? If something that is not even posted then how it will be identified as checked?? – Imad Oct 10 '16 at 14:33
  • @Imad Thanks. I didn't realise they weren't getting posted to the server. I have a solution to get around it now. Thanks. – Pindub_Amateur Oct 10 '16 at 15:47
1

If the ListItems of the CheckBoxList are added with aspnet, either in code behind or the .aspx page, ViewState will ensure that it will see those disabled checkboxes as checked, even if they are not send to the server.

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> selectedValues = new List<string>();
        Label1.Text = "";
        foreach (ListItem item in myCheckBoxList.Items)
        {
            if (item.Selected)
            {
                selectedValues.Add(item.Value);
                Label1.Text += item.Value + "<br>";
            }
        }
    }

And to make the example complete:

    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br /><br />
    <asp:CheckBoxList ID="myCheckBoxList" runat="server">
        <asp:ListItem Text="Blueberry" Value="Blueberry"></asp:ListItem>
        <asp:ListItem Text="Raspberry" Value="Raspberry"></asp:ListItem>
        <asp:ListItem Text="Blackberry" Value="Blackberry"></asp:ListItem>
        <asp:ListItem Text="Strawberry" Value="Strawberry" Enabled="false" Selected="True"></asp:ListItem>
        <asp:ListItem Text="Gooseberry" Value="Gooseberry" Enabled="false" Selected="True"></asp:ListItem>
    </asp:CheckBoxList>
    <br /><br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Tested it myself before posting. I can guarantee you that it does. Maybe your problem is somewhere else, like in the biding of the ListItems. – VDWWD Oct 10 '16 at 16:29