0

How can I get a handle on my user control in 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
  • What if you set Enabled to true -- can you get a handle to your user control? I think if enabled is false, the control isn't rendered. Could be wrong there, though. Commented Jun 18, 2012 at 20:05

1 Answer 1

2

In order to reference the ClientID like you did, the JavaScript needs to be inside the UserControl, not the page. If you want to use it the way you have it here, you'll need to expose the clientId as a public property from your user control:

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

and modify your script:

document.getElementById('<% =WebUserControl1.userCtlClientId%>');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.