如何获取asp.net客户端ID在外部JavaScript文件 [英] How to get asp.net client id at external javascript file
问题描述
当我使用嵌入的的JavaScript 的功能的我能得到的客户端ID 的这个code元素:
When I use embedded javascript functions I can get client id of elements with this code:
document.getElementById('<%=buttonXXX.ClientID%>' )
但现在我使用的外部JavaScript文件的缓存和更快的渲染
这code不工作了用于获取的客户端ID的元素的,它给了错误。
But now I am using external javascript file for caching and faster rendering and this code does not work any more for getting client id's of elements, it gives error.
我怎样才能获得的客户端ID的的元素在的外部JavaScript文件的使用
asp.net 2.0,.net框架3.5,C#,IIS 7.5
How can I get client id's of elements at external javascript file using asp.net 2.0 , netframework 3.5 , c# , iis 7.5
推荐答案
我可以建议2种方式。
定义变量调用JavaScript之前。
define your variables before call the javascript.
var ButtonXXXID = <%=buttonXXX.ClientID%>
// and now include your javascript and use the variable ButtonXXXID
方式二
在外部JavaScript文件,写你的code为:
Second way
in the external javascript file, write your code as:
function oNameCls(ControlId1) {
this.ControlId1 = ControlId1;
this.DoYourWork1 = function() {
// use the control id.
// this.ControlId1
}
}
和调用你的行动等。
<script>
// init - create
var <%=this.ClientID%>MyCls = new oNameCls(<%=Control1.ClientID%>);
// do your work
<%=this.ClientID%>MyCls.DoYourWork1();
</script>
在调用操作这种方式,您prevent覆盖与来自其他控件在同一页面上相同的动作一个控制一个动作。
calling the action this way you prevent overwrite one action from one control with the same action from other controls on the same page.
这篇关于如何获取asp.net客户端ID在外部JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!