Properly referencing controls in ASP.NET user controls in JavaScript

后端 未结 1 1942
猫巷女王i
猫巷女王i 2021-01-15 01:42

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

相关标签:
1条回答
  • 2021-01-15 02:19

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

    <asp:TextBox runat="server" ID="myTextBox" />
    <asp:Button runat="server" ID="myButton" Text="click" />
    
    <script language="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

    0 讨论(0)
提交回复
热议问题