Solved

Changing Input Disabled property via JavaScript problem

Posted on 2004-10-08
10
1,285 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now