Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  7] [ 2]  / answers: 1 / hits: 15836  / 12 Years ago, thu, august 16, 2012, 12:00:00

I have Googled this to death and found lots of 'answers', but none of them will work for me, so maybe you clever people could give me a 'definitive' answer?



I have a Javascript function:



function enableSaveLink() {
document.getElementById('<%= btnSaveLink.ClientID %>').removeAttribute('disabled');
}


This works fine, but obviously it is hard-coded to enable a particular control on my page. What I'd like is to be able to call this function from any control on the page, passing the name of the control I'd like to enable as a variable. So, in an ideal world, my Javascript function would look like this:



function enableControl(ctl) {
document.getElementById(ctl).removeAttribute('disabled');
}


And I'd call it like this:



<asp:button id=btnTestButton runat=server Text=Click Me onclientclick=enableControl('txtTestTextbox') />
<asp:button id=txtTestTextbox runat=server enabled=false />


I know the way I've passed the control name would never work, but I've tried passing it in all different ways and none work, so this is just for the purposes of illustration. Can anyone tell me how to actually make this work?


More From » asp.net

 Answers
6

Ok, I've cracked it. There are probably more ways than one to do this, but this is fairly elegant.



My Javascript function:



    function enableControl(ctl) {
document.getElementById(ctl).removeAttribute('disabled');
}


My ASP.NET markup:



<asp:Button ID=btnTestButton runat=server Text=Click to enable OnClientClick=enableControl('txtTestTextbox'); />
<asp:TextBox ID=txtTestTextBox runat=server enabled=false ClientIDMode=Static />


The key is the ClientIDMode property, which, when set to static, means that the control's client-side ID when it is rendered will match the ID you give it in markup. If it's within a naming container you may need to include that in the variable passed in the function call. See here for more info about ClientIDMode.



Anyway, this works for me! Thanks for your input, everyone.


[#83603] Tuesday, August 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bobbidayanam

Total Points: 82
Total Questions: 99
Total Answers: 96

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
bobbidayanam questions
Sat, Mar 21, 20, 00:00, 4 Years ago
Mon, Dec 24, 18, 00:00, 6 Years ago
Fri, Nov 16, 18, 00:00, 6 Years ago
;