获取的clientid从外部JavaScript文件中的用户控件 [英] Get clientid in user control from external javascript file

查看:115
本文介绍了获取的clientid从外部JavaScript文件中的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发ASP.NET它使用JavaScript操纵控制的用户控件(ASCX)。目前的JavaScript code为内联,并使用<%= somecontrol.ClientID%GT; 来获得它需要的控

I am developing a user control (ascx) in ASP.NET which uses javascript for manipulating controls. Currently the javascript code is inlined and uses <%= somecontrol.ClientID %> to get the control it needs.

我希望把JavaScript文件中的外部文件,但来自外部文件我不能使用上述语法检索控制。我已阅读关于<一个可能的解决方案href=\"http://stackoverflow.com/questions/1861103/how-can-i-access-server-controls-from-my-external-javascript-file/1861211#1861211\">this和<一个href=\"http://stackoverflow.com/questions/2078392/can-i-count-on-ctl00-pageplaceholder-myid-staying-the-same/2078420#2078420\">this答案,但问题是,用户控制可以放置多次页面上。这意味着,控制阵列(在答案提到的)将与不同项被呈现几倍。其结果是,该脚本将不能够检索它需要的标识。如果我把&LT;%=客户端Id%方式&gt; 在数组保存的物品,然后我就会有同样的问题,因为我试图解决的名称

I want to put the javascript file in external file but from external file I cannot use the above syntax for retrieving controls. I have read about possible solutions in this and this answers but the problem is that the user control can be placed multiple times on page. This means that the Controls array (mentioned in the answers) will be rendered several times with different items. As a result the script will not be able to retrieve the id it needs. If I put <%= ClientId %> in the name of array that holds items then I will have the same problem as I am trying to solve.

任何想法?

推荐答案

好吧,不同的方法,我尝试使用JavaScript类样式,然后初始化它为每个控制。

Ok, a different approach, that I try to use a JavaScript-class style, and then initialize it for each control.

在外部JavaScript文件,写你的code为:

In the external javascript file, write your code as:

function oNameCls(ControlId1) {

    this.ControlId1 = ControlId1;

    this.DoYourWork1 = function() {
        // use the control id.
        // this.ControlId1
    }    
    this.DoYourWork2 = function() {
        // use the control id.
        // this.ControlId1
    }    

}

和在控制做这样的电话。

And on the control do the call like that.

<script language="Javascript" type="text/javascript">
    // init - create
    var <%=this.ClientID%>MyCls = new oNameCls(<%=Control1.ClientID%>);
    // do your work
    <%=this.ClientID%>MyCls.DoYourWork1();
</script>

现在希望更好的帮助。

Hope now help better.

这篇关于获取的clientid从外部JavaScript文件中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆