Skip to content Skip to sidebar Skip to footer

Properly Referencing Controls In Asp.net User Controls In Javascript

I have an ASP.NET user control that contains a text box control and a button control. This user control will be added to my web-page many times. I need to have a piece of JavaScr

Solution 1:

You can use ClientId property of controls (it's uniquely identifies control on the client side) and make something like that:

<asp:TextBoxrunat="server"ID="myTextBox" /><asp:Buttonrunat="server"ID="myButton"Text="click" /><scriptlanguage="javascript"type="text/javascript">document.getElementById("<%=myTextBox.ClientID %>").onclick = function() {
        document.getElementById("<%=myButton.ClientID %>").disabled = "disabled";
    }
</script>

Also refer to this document: Client Script in ASP.NET Web Pages, section Referencing Server Controls in Client Script

Post a Comment for "Properly Referencing Controls In Asp.net User Controls In Javascript"