Ram
Ram

Reputation: 933

get the serverside control id in javascript in asp.net?

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.

Upvotes: 2

Views: 33515

Answers (3)

bendi
bendi

Reputation: 133

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 .

Upvotes: 0

suyog
suyog

Reputation: 52

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 ;

Upvotes: 4

Michael Christensen
Michael Christensen

Reputation: 1786

Don'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

Upvotes: 10

Related Questions