amiu

amiu

  • NA
  • 13
  • 4.3k

Using jQuery to reference ASP.NET controls

Apr 30 2012 12:46 PM
How to reference asp.net controls by Javascript and Jquery?

Steps:

1.Find out the asp.net control id - <%=[control name].ClientID %>.
2.Reference it using JavaScript getElementById(<%=[control name].ClientID %>) or JQuery by $('#<%=[control name].ClientID %>').

Example:

Make sure you put the jquery reference in the header:
 <script type="text/javascript" src="[location]/jquery-1.7.1.min.js"></script>

ASP.Net:

<asp:TextBox ID="myTextBox"  runat="server" Width="600" />
   
JAVASCRIPT:

<Script>
var myTextBox = document.getElementById("<%=myTextBox.ClientID %>");
</Script>

JQuery:

<Script>
var
myTextBox = $('#<%=myTextBox.ClientID %>').change(function () { alert('test'); });
</Script>






Answers (1)