locked
Code-Behind Syntax for Checkbox.Enabled = False RRS feed

  • Question

  • User1110261639 posted

    I need to disable a checkbox in the Code-Behind, but be able to enable it with JavaScript on the Client Side.

    I have the JS down and it will work as long the checkbox is not disabled on the code-behind.

     

    Code-Behind things I've Tried

    chkIsNetChange.Attributes.Add("disabled", "true")  'Doesn't work
    chkIsNetChange.Enabled = False  'Doesn't work
    chkIsNetChange.Style.Add("disabled", "true")

     

    How can I disable the checkbox in the Code Behind and still be able to enable it with java script on the client side?

     

    Thanks,
    matchbx


     

    Tuesday, April 18, 2006 11:43 AM

All replies

  • User798903548 posted

    CodeBehind:
    chkIsNetChange.Enabled = False 

    JavaScript:
    <script language="JavaScript">
    <!--
    function enableCheckBox()
    {
     document.getElementById('chkIsNetChange').parentElement.disabled = false;
     document.getElementById('chkIsNetChange').disabled = false;
    }
    // -->
    </script>

    NC...

    Tuesday, April 18, 2006 12:43 PM
  • User1110261639 posted

    .parentElement.disabled is the part I was missing.  Once I added that line of code to the JS Function...everything worked great.

     

    Thanks a lot.

    matchbx

    Tuesday, April 18, 2006 12:59 PM
  • User-112528313 posted
    Thank you soooo much. I have been stuck on this issue for the past 3 days. This answered my question as well.
    Tuesday, April 18, 2006 2:34 PM
  • User798903548 posted

    When you disable a CheckBox through it's server-side property, .NET adds a <span> tag around it so that any attachments are also disabled.
         <span disabled="disabled"><input id="CheckBox1" type="checkbox" name="CheckBox1" disabled="disabled" /></span>

    Doing a View Source with the page up in your browser would have shown you the above control declaration.

    NC...

    Wednesday, April 19, 2006 7:50 AM
  • User51359218 posted

    disabled=false to parent element seems the only way for IE8, but it does not work in FireFox. Firefox directly chages the "disabled" property and works fine. It hangs up when it tries to change the disabled attribute of the parent element. So a null checking to parent element before changing it, works both in IE8 and FF.

    if(document.getElementById('chkIsNetChange').parentElement!=null){
    
    document.getElementById('chkIsNetChange').parentElement.disabled = false;
    
    }
    
    document.getElementById('chkIsNetChange').disabled = false;


    Monday, May 3, 2010 2:05 AM
  • User1082398491 posted

    For tackling the same issue for a RadioButtonList use

    RadioButtonList.Attributes.Add(”disabled”, “disabled”);

    in stead of:

    RadioButtonList.Enabled = false;

    in code behind.

    Source: http://www.codefrenzy.net/2008/06/19/enabling-disabling-aspnet-radio-buttons-using-code-behind-and-javascript/comment-page-1/#comment-225

    Thursday, June 24, 2010 10:20 AM