如何在外部 javascript 文件中获取 asp.net 客户端 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3845900/
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
How to get asp.net client id at external javascript file
提问by MonsterMMORPG
When I use embedded javascriptfunctionsI can get client idof elements with this code:
当我使用嵌入式javascript函数时,我可以使用以下代码获取元素的客户端 ID:
document.getElementById('<%=buttonXXX.ClientID%>' )
But now I am using external javascript filefor caching and faster rendering and this code does not work any more for getting client id'sof elements, it gives error.
但是现在我使用外部 javascript 文件进行缓存和更快的渲染,并且此代码不再用于获取元素的客户端 ID,它给出了错误。
How can I get client id'sof elements at external javascript fileusing asp.net 2.0 , netframework 3.5 , c# , iis 7.5
如何使用 asp.net 2.0、netframework 3.5、c#、iis 7.5在外部 javascript 文件中获取元素的客户端 ID
回答by Aristos
I can suggest 2 ways.
我可以建议 2 种方法。
First way
第一种方式
define your variables before call the javascript, inside the .aspxfile that can be compiled.
在可以编译的.aspx文件中调用 javascript 之前定义变量。
var ButtonXXXID = <%=buttonXXX.ClientID%>
// and now include your javascript and use the variable ButtonXXXID
Second way
第二种方式
in the external javascript file, write your code as:
在外部 javascript 文件中,将您的代码编写为:
function oNameCls(ControlId1) {
this.ControlId1 = ControlId1;
this.DoYourWork1 = function() {
// use the control id.
// this.ControlId1
}
}
And call your actions like.
并称您的行为为。
<script>
// init - create
var <%=this.ClientID%>MyCls = new oNameCls('<%=Control1.ClientID%>');
// do your work
<%=this.ClientID%>MyCls.DoYourWork1();
</script>
calling the action this way you prevent overwrite one action from one control with the same action from other controls on the same page.
以这种方式调用动作可以防止用同一页面上其他控件的相同动作覆盖一个控件的一个动作。
回答by Darin Dimitrov
You could use a class selector. jquerymight greatly simplify your life here. So you could apply a special class to the control:
您可以使用类选择器。jquery可能会大大简化您的生活。所以你可以对控件应用一个特殊的类:
<asp:LinkButton ID="foo" CssClass="foo" runat="server" Text="foo" />
and in your external javascript file once the DOM is ready you could reference the button using a class selector:
并且在 DOM 准备就绪后,在您的外部 javascript 文件中,您可以使用类选择器引用按钮:
$(function() {
var fooButton = $('.foo');
});
回答by kein
in script file (test.js
)
在脚本文件 ( test.js
)
function test(ControlID1) {
var controlId = document.getElementById(ControlID1);
controlId.onchange = function () {
alert(controlId.value);
}
}
in .aspx
file
在.aspx
文件中
<script type="text/javascript">
var callTest = test('<%=txtSelected.ClientID%>');
window.onload = callTest;
</script>
回答by s k
I use the following code in my .js
file, when I have no other better choice.
.js
当我没有其他更好的选择时,我在我的文件中使用以下代码。
$("[id$='buttonXXX'])
回答by GermanSniper
I know old question but with jquery u can use this approache:
我知道老问题,但使用 jquery 你可以使用这种方法:
Aspx file
aspx文件
<asp:Button ID="btnCalculate" ClientID="btnCalculate" runat="server" />
external js file
外部js文件
$("[ClientID='btnCalculate']").
https://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/
https://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/