如何在javascript中获取用户控件

时间:2012-06-18 20:01:53

标签: 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="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>

1 个答案:

答案 0 :(得分:2)

为了像你一样引用ClientID,JavaScript需要在UserControl中,而不是页面。如果你想在这里使用它的方式,你需要将clientId从你的用户控件公开为公共属性:

public string userCtlClientId
{
  get{return userCtl.ClientId;} 
}

并修改您的脚本:

document.getElementById('<% =WebUserControl1.userCtlClientId%>');
相关问题