Javascript 在asp.net 中的javascript 中获取服务器端控件ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6716549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:54:55  来源:igfitidea点击:

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

javascriptasp.net

提问by Ram

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.

我在 asp:table 中有 asp:ImageButton。我想在 javascript 中获取按钮的 id,我想根据条件启用按钮。我取得了怎样的成就。我试过如下。

var btn;
btn = document.getElementById('<%= generateRptbtn.ClientID%>').value;
btn.enabled = true;

But I'm getting empty as btn value.

但是我的 btn 值越来越空了。

回答by Michael Christensen

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

这样你就得到了实际的按钮对象。Value 将返回由 asp 按钮呈现的 html 输入元素的 value 属性的内容

Also I believe in javascript you would set disabled to false rather than enabled to true

此外,我相信在 javascript 中,您会将禁用设置为 false 而不是启用为 true

回答by suyog

Check out this article. There are 2-3 ways to get ID of a control in a javascript

看看这篇文章。有 2-3 种方法可以在 javascript 中获取控件的 ID

http://codedotnets.blogspot.in/2012/01/how-get-id-server-control-javascript.html

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 ;

还有一件事,一旦禁用控件,就无法获得该控件的 ID 或值。button.disable = false 也是如此;

回答by bendi

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

我在 MSDN 站点上找到了另一个解决方案。我认为这种方式非常简单且“干净”。我在 asp.net 4.0 中使用它,我不确定它是否适用于较低版本。

如果像这样在 aspx 代码中将 Button 的属性 ClientIDMode 设置为“Static”

<asp:Button ClientIDMode="Static" runat="Server" ID="generateRptbtn" />

then in your .js code you can just call

然后在你的 .js 代码中你可以调用

var btn = document.getElementById('generateRptbtn');
btn.disabled = false;


And as mentioned latr0dectus, you can't call


正如提到的latr0dectus,你不能打电话

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.


这是 MSDN 站点的链接http://msdn.microsoft.com/en-us/library/dd410598(v=vs.100).aspx