最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get the serverside control id in javascript in asp.net? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 10

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

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 .

发布评论

评论列表(0)

  1. 暂无评论