javascript 如何使用java脚本查找asp控件ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10618196/
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-10-26 10:27:52  来源:igfitidea点击:

How to find a asp control id using java script?

javascriptasp.net

提问by user1369925

I am using following code to find control id.

我正在使用以下代码来查找控件 ID。

alert(document.getElementById("<%# TextBox1.ClientId %>").value )

but this code is giving error "object required". Please help me.

但是这段代码给出了错误“需要对象”。请帮我。

采纳答案by Habib

Either use

要么使用

alert(document.getElementById("<%= TextBox1.ClientId %>").value )

or set ClientIDMode="Static"for textbox and then

或设置 ClientIDMode="Static"为文本框,然后

alert(document.getElementById("<%= TextBox1 %>").value )

Also check How to: Access Controls from JavaScript by ID

还要检查如何:通过 ID 从 JavaScript 访问控制

回答by Josh Darnell

You need to use '=', not '#'

您需要使用“=”,而不是“#”

alert(document.getElementById("<%= TextBox1.ClientId %>").value );

The "<%#" symbol is an inline expression used for databinding.

" <%#" 符号是用于数据绑定的内联表达式。

The "<%=" symbol there is used for display / translation purposes. It basically does a Response.Writeof just the value of the .ClientIDproperty of your server control.

<%=”符号用于显示/翻译目的。它基本上Response.Write只执行.ClientID服务器控件的属性值。

See this article on inline expressions in asp.netfor more info.

有关详细信息,请参阅有关asp.net 中的内联表达式的这篇文章

回答by Imran Rizvi

replace # with = in the given statement updated statement is

在给定的语句中用 = 替换 # 更新语句是

  alert(document.getElementById("<%= TextBox1.ClientId %>").value);

回答by Saurabh

alert(document.getElementById('Id of control').value )

You can get the exact id of control by view source.

您可以通过查看源获得控件的确切 id。

回答by Andrei

In the context of JavaScript, which is on the client side "<%# TextBox1.ClientId %>"has no meaning because this will be translated by ASP into to a different ID based on how you configure TextBox1.ClientIDModefor which you have 4 modes described here Control.ClientIDMode

在 JavaScript 的上下文中,客户端"<%# TextBox1.ClientId %>"没有任何意义,因为这将被 ASP 根据您的配置TextBox1.ClientIDMode方式转换为不同的 ID ,您在这里描述了 4 种模式Control.ClientIDMode