Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中获取usercontrol_Javascript_Asp.net - Fatal编程技术网

如何在javascript中获取usercontrol

如何在javascript中获取usercontrol,javascript,asp.net,Javascript,Asp.net,如何在JavaScript中获得用户控件的句柄 <body> <form id="form1" runat="server"> <div> <uc1:WebUserControl ID="WebUserControl1" runat="server" Enabled="False" /> <input id="Button1" type="button" value="button" runat

如何在JavaScript中获得用户控件的句柄

<body>
    <form id="form1" runat="server">
    <div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" Enabled="False" />
        <input id="Button1" type="button" value="button" runat="server" onclick="return Button1_onclick()" /></div>
    </form>

    <script type="text/javascript">
        var pageDefault = {
            uc: document.getElementById("<%=WebUserControl1.ClientID%>"),
            btn1: document.getElementById("<%=Button1.ClientID%>"),
            init: function() {
                this.btn1.onclick = function() {
                    uc.setAttribute('Enabled', 'True'); //uc is null at this point
                };
            }
        }
        pageDefault.init();
    </script>

</body>

var pageDefault={
uc:document.getElementById(“”),
btn1:document.getElementById(“”),
init:function(){
this.btn1.onclick=函数(){
setAttribute('Enabled','True');//此时uc为空
};
}
}
pageDefault.init();

为了像您那样引用ClientID,JavaScript需要在UserControl中,而不是页面中。如果要按此处的方式使用它,则需要从用户控件中将clientId公开为公共属性:

public string userCtlClientId
{
  get{return userCtl.ClientId;} 
}
并修改您的脚本:

document.getElementById('<% =WebUserControl1.userCtlClientId%>');
document.getElementById(“”);

如果将
Enabled
设置为true,会怎么样?您能获得用户控件的句柄吗?我认为如果
enabled
为false,则不会呈现控件。不过,这可能是错误的。