0

I have a CheckBoxList which can load as disabled, but might need to become enabled after some client side actions.

I am able to enable the checkboxes using jQuery, but the problem is that after submitting the page, the selected items in the ChceckBoxList are not recognized (probably because the control is not in the ViewState).

A simple example for the scenario:

<asp:CheckBoxList ID="chkList1" runat="server" Enabled="false" ClientIDMode="Static">
       <asp:ListItem Value="123" />
       <asp:ListItem Value="456" />
       <asp:ListItem Value="789" />
</asp:CheckBoxList>

$(document).ready(function()
{
    $("#divEnable").click(function()
    {
        var inputs = $("#chkList1").find("input");
        for (var i = 0; i < inputs.length; i++)
        {
            inputs[i].disabled = false;
        }
    });
}

And then after enabling the checkboxes, selecting them and sending a postback - no selected items are recognized.

I tried disabling the items "manually", as in

chkList1.Items[0].Attributes.Add("disabled", "disabled");

But that did not work (the disabled attribute was attached to the containing span instead of the input control).

I also considered using hidden fields to keep track of the checkbox selections, but since I have multiple CheckBoxLists in a grid, that would be very inelegant.

Is there any way around this?

2
  • You should show the HTML that is generated and ends up in the browser, not the ASP that creates it. Commented May 16, 2012 at 10:53
  • It's just a table where each row contains a checkbox input and a corresponding label within a span. When Enabled="False", the inputs have disabled="disabled". Commented May 16, 2012 at 10:56

3 Answers 3

1

try this:

$(document).ready(function()
{
    $("#divEnable").click(function()
    {
        var inputs = $("#chkList1").find("input")
            .each(function(){
                            //if($(this).is(":disabled"))
                            //{
                               $(this).prop("disabled",true);
                            //}
                          });      
});

Ref:
jQuery asp control .prop(“disabled”, “”) not enabling check box in IE 9
Unable to handle disable check box

Sign up to request clarification or add additional context in comments.

1 Comment

I think it should be $(this).prop("disabled",false). Anyway, my problem is not enabling the checkboxes (they're clickable after my click event executes), but the fact that the selected items are not recognized in the server side after a postback. I'll edit the question to make it clearer.
0

You can use this

  for (i = 0; i < document.forms[0].length; i++)
   {
      e = document.forms[0].elements[i];
      if (e.id.indexOf("chklist") != -1)
      {
           e.disabled = false;
      }
  }

Comments

0

I had the same issue. The way I fixed this was to enable the checkboxlist to start, then on load of the page, disable using script. That way the controls got into the viewstate, but were still disabled to start.

    var checkBoxListToChange = document.getElementById('');
    var checkboxes = checkBoxListToChange.getElementsByTagName("input");
    for (var x = 0; x < checkboxes.length; x++) {
        checkboxes[x].disabled = true;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.