I have asp:ImageButton in asp:table. I want to get the id of button in javascript and I want to enabled the button based on conditon. How cloud I acheive. I tried as below.
var btn;
btn = document.getElementById('<%= generateRptbtn.ClientID%>').value;
btn.enabled = true;
But I'm getting empty as btn value.
I have asp:ImageButton in asp:table. I want to get the id of button in javascript and I want to enabled the button based on conditon. How cloud I acheive. I tried as below.
var btn;
btn = document.getElementById('<%= generateRptbtn.ClientID%>').value;
btn.enabled = true;
But I'm getting empty as btn value.
Share Improve this question edited Jul 16, 2011 at 9:51 VMAtm 28.4k17 gold badges83 silver badges132 bronze badges asked Jul 16, 2011 at 9:35 RamRam 9336 gold badges17 silver badges36 bronze badges3 Answers
Reset to default 10Don't use value.
var btn = document.getElementById('<%= generateRptbtn.ClientID%>');
btn.disabled = false;
This way you get the actual button object. Value would return the contents of the value property of the html input element rendered by the asp button
Also I believe in javascript you would set disabled to false rather than enabled to true
Check out this article. There are 2-3 ways to get ID of a control in a javascript
http://codedotnets.blogspot.in/2012/01/how-get-id-server-control-javascript.html
and one more thing once you disable the control, its not possible to get the ID or value of that control. So do button.disable = false ;
I found another solution on MSDN site. I think this way is pretty easy and "clean". I use it in asp.net 4.0, I'm not sure, if it works in lower versions.
If you set Button's property ClientIDMode to "Static" in your aspx code like this
<asp:Button ClientIDMode="Static" runat="Server" ID="generateRptbtn" />
then in your .js code you can just call
var btn = document.getElementById('generateRptbtn');
btn.disabled = false;
And as mentioned latr0dectus, you can't call
var btn = document.getElementById('generateRptbtn').value;
Here's a link to the MSDN site http://msdn.microsoft.com/en-us/library/dd410598(v=vs.100).aspx .