Reputation: 15423
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>
Upvotes: 0
Views: 4114
Reputation: 180
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%>');
Upvotes: 2