Changing Input Disabled property via JavaScript problem

I tried this question in Web Dev yesterday and got no response so...

I have 2 types of JavaScript actions that I'm taking:

1.  When a checkbox is checked, a textbox becomes "enabled"...when unchecked, "disabled".  This works fine.

2.  When a checkbox is checked, a checkboxlist (an array of checkboxes) becomes "enabled"...when unchecked, "disabled".  This works fine, UNLESS I set the .Disabled property of the checkboxlist to =true in the code-behind.

Now, I am setting the .Disabled property =true to both the textbox and the checkboxlist in the code-behind on page_load.  This renders the following HTML:
<td colspan="2"><a name="2"><textarea name="txtCSAS" rows="2" id="txtCSAS" disabled="disabled" class="normalText"></textarea></a></td>

Checkboxlist (one of the checkboxes):
<td><span disabled="disabled"><input id="chklstMB_0" type="checkbox" name="chklstMB:0" disabled="disabled" /><label for="chklstMB_0">MBTest 0</label></span></td>

The JavaScript DOES work if I do NOT set the .Disabled property =true in the code-behind, but I must have control of the .Disabled in the code-behind and in JavaScript.
The difference in the rendered HTML is that when I set the .Disabled property =true in the code-behind, disabled="disabled" is rendered.  When I don't, then no disabled property is rendered.

I'm not going to post the JavaScript unless it's asked for, for the sake of space.  The meat of it does this:
textBox.disabled=false; or textBox.disabled=true;

Checkboxlist (iterate thru the array of form "input" controls):
arr[i].disabled=false; or arr[i].disabled=true;

How can I have control via both JavaScript (client side) and code-behind (server side)?

Thank you.
Who is Participating?
David H.H.LeeConnect With a Mentor Commented:
sjd0103 ,
I know that have no problem with textbox control. However, i do find a way how to set disabled property via code-behind, not use javascript.

Instead of
chklst2.Enabled = False

Change to
chklst2.Attributes.Add("disabled", "true")

The rest is work as you though. I think this is the easiest way to solve the problem. Work on code-behind and javascript based. Hope that convinced you.

Set the "RunAtServer" property to TRUE if you want to access any control in ur codebehind.
From Javascript, use:   document.FindElementById('textBox1').disabled=true
sjd0103Author Commented:
The CheckBoxList that I'm using is being run server side.  The <input> tag that I included above is just one of the checkboxes that is rendered as a part of the .NET CheckBoxList....(.NET is rendering HTML based on the type of control used...the list of <input> controls that represents a .NET CheckBoxList since there is not one in HTML).

Anyway, the JavaScript has the correct object and can disable/enable it just fine as long as I DON'T set the disabled property to true in the codebehind on page_load or postback.

Again, if I set the CheckBoxList.Disabled = true in codebehind, the JavaScript .disabled =true (or false) does not change anything.  If never do anything with the control via codebehind then the JavaScript works fine.  

How can I have them both working together?  I am doing the exact same thing with TextBoxes and not having this issue.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

David H.H.LeeCommented:
sjd0103 ,
If that is your problem, let see this solution. You can use javascript to disable your web control via code-behind

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            Dim ar As New ArrayList
            chkLstEE.DataSource = ar

            Dim jsScript As String
            jsScript = "<script language='javascript'>"
            jsScript += "document.getElementById('txtEE').disabled=true;" & vbCrLf
            jsScript += "document.getElementById('chkLstEE').disabled=true;" & vbCrLf
            jsScript += "</script>" & vbCrLf

            Page.RegisterStartupScript("disableControl", jsScript)
        End If

        'choose check box to enable textbox
        chkEE.Attributes.Add("onClick", "javascript:document.getElementById('txtEE').disabled=(document.getElementById('txtEE').disabled)==false?true:false;")

        'choose check box to enable checkbox list
        chkEE2.Attributes.Add("onClick", "javascript:document.getElementById('chkLstEE').disabled=(document.getElementById('chkLstEE').disabled)==true?false:true;")
    End Sub

sjd0103Author Commented:
x_com...that did work to change the disabled property of the checkboxlist.

I added chkLstEE.Enabled=false; (I'm using C# by the way...but it's not a problem for me to swap between the two)
to the end of page_load and that breaks the JS again.

This is driving me nuts.
David H.H.LeeCommented:
If you set chkLstEE.Enabled=false via code-behind, you cant enable it back on front-end. The only solution i can see is dont set any property for checkboxlist Enabled property in code-behind. But, the problem is you disable it for the first page load. So, I do suggest you disable it using js via code-behind at page_load. That work fine on me.
sjd0103Author Commented:
I agree that I may be able to work around the issue using JavaScript.  That leaves me with that sinking feeling that I've created a workaround for something simple that I just overlooked.

Why is it that a textbox (set to .Disabled=true in code-behind) can be enabled via JS but an checkbox (set to .Disabled=true in code-behind) cannot?
David H.H.LeeCommented:
sjd0103 ,
I'm not sure what happend there. But for sure that something related with .net framework itself. After checkboxlist rendered in browser, you'll see it become a table consist of a multiple checkbox item. All the checkbox item had a property for disabled='disabled'. I think if you want to enable it, you got to enable all the item one by one. But, the things is you need to loop all the item with the generated checkbox and disable all the item as well. The problem is all the generated item is client id. However, if you want to set disabled via code-behind, you can simply add your checkboxlist in others html object like <span>,<div>... I think this is the most efficient way to solve it more faster?
<div id="divLst" runat="server">

divLst.Disabled = True

chkEE2.Attributes.Add("onClick", "javascript:document.getElementById('divLst').disabled=(document.getElementById('divLst').disabled)==true?false:true;")

Hope that solve your doubt.


sjd0103Author Commented:
I appreciate your time and posting, but I still am not convinced.

I have the JavaScript to loop the <input> controls and enable/disable the checkbox "list" items.  That works fine as long as I don't set it in code-behind.

Agreed that the property disabled='disabled' when rendered in HTML from .NET when I set .Disabled=true in the code-behind...BUT, the same is true for the textbox...and I CAN control that with JavaScript.

That's what's getting me here...the fact that I can do the exact same thing to the textbox, but not the list of checkbox items.  Am I making sense?
sjd0103Author Commented:
I've created a solution that works.  When adding chkLstEE.Attributes.Add("disabled", "true"), .NET renders a table with id = chkLstEE and disabled = true.  I then change that disabled property via JavaScript.  This still cannot be controlled reliably with both JS and code-behind.

x_com, I'll give you the points for the effort (thank you, I don't mean to seem ungrateful).  If anyone else has a solution, let me know and I'll open a new question!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.