Solved

Changing Input Disabled property via JavaScript problem

Posted on 2004-10-08
10
1,294 Views
Last Modified: 2008-03-04
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:
Textbox:
<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:
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.
0
Comment
Question by:sjd0103
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12257967
Set the "RunAtServer" property to TRUE if you want to access any control in ur codebehind.
From Javascript, use:   document.FindElementById('textBox1').disabled=true
-Baan
0
 

Author Comment

by:sjd0103
ID: 12258343
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.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12259010
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
            ar.Add("1")
            ar.Add("2")
            ar.Add("3")
            chkLstEE.DataSource = ar
            chkLstEE.DataBind()

            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

Regards
x_com
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:sjd0103
ID: 12260486
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.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12260870
sjd0103,
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.
0
 

Author Comment

by:sjd0103
ID: 12260989
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?
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12261192
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?
eg:
<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.

Regards
x_com
   

                  
0
 

Author Comment

by:sjd0103
ID: 12261259
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?
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 300 total points
ID: 12261398
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.

code-behind
-----------------------
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.

Regards
x_com
0
 

Author Comment

by:sjd0103
ID: 12298035
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!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question