29

How to access asp.net control using jquery

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

$('#myTextBox') wouldn't work.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • I have seen many questions similar to this while reviewing questions in the last couple of days. There is sufficient information about this but I decided to write a comprehensive list of things here. Hope this helps. – Venkata Krishna Nov 26 '13 at 20:22

3 Answers3

87

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

The above aspx code when rendered on a page changes to

<input type="text" id="ctl00_Main_myTextBox" name="ctl00$Main$myTextBox" />

This is because the master and control information in which the .net control resides gets prepended which makes it a little tricky for us to write a selector.

You have a few options. This is by no means comprehensive, but I will give it a try.

Option1:

$('#<%= myTextBox.ClientID %>')

Use the ClientID - recommended but meh.. not so much. I would try to avoid writing ClientID if I could. The primary reason being, you can only use it in .aspx pages and not external .js files.

Option2:

$('[id$=myTextBox]') // id which ends with the text 'myTextBox'

$('[id*=myTextBox]') // id which contains the text 'myTextBox'

Using attribute selectors - recommended too, looks a bit ugly but effective.

I have seen a few questions here, worrying about performance with these selectors. Is this the best way possible? No.

But, most of the time you won't even notice the performance hit, unless of course, your DOM tree is huge.

Option3:

Using CssClass - highly recommended. Because selectors using classes are clean and uncomplicated.

In case you are wondering, CssClass for .net controls is the same as class for traditional html controls.

<asp:TextBox runat="server" ID="myTextBox" CssClass="myclass" /> //add CssClass

$('.myclass') //selector

Option4:

Use ClientIDMode="Static", which got introduced in .NET Framework 4.0, on the control so that it's ID will stay unchanged. - recommended too.

<asp:TextBox runat="server" ID="myTextBox" ClientIDMode="Static"  /> //add ClientIDMode

$('#myTextBox') //use the normal ID selector

Note: In my experience, I have seen ugly selectors like $('#ctl00_Main_myTextBox'). This is the result of directly copy pasting the ID rendered from the page and use it in the script. Look, this will work. But think about what will happen if the control ID or the master ID changes. Obviously, you will have to revisit these IDs and change them again. Instead of that, use one of the options above and be covered.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • 1
    Quick innocent question : what happens with option 4 if you do this in a control within a usercontrol, and you add this usercontrol twice on a page. Is there some kind of validation on runtime or will it simply cause id duplication in the resulting HTML ? – Laurent S. Apr 09 '14 at 14:03
  • 1
    @Bartdude - it will cause duplication in the resulting HTML. `ClientIDMode` just keeps the existing ID so you have to be careful while differentiating which id is clicked. Using classes and/or data-attribute selectors would probably be a good option in this case. – Venkata Krishna Apr 15 '14 at 15:32
  • 1
    Thanks ! Exactly what I thought. I guess you can't have it all : security and flexibility. – Laurent S. Apr 16 '14 at 11:56
  • Just to be syntactically correct as per JQuery documentation, the actual IDs should be in quotes. $("[id$='myTextBox']") $("[id*='myTextBox']") – Moiz Tankiwala Dec 23 '16 at 06:33
1

In addition to the answer by krishna you can use name attribute instead when it is rendered in HTML by IIS the name attribute value assigned should be the same as the ID value:

Example

<asp:TextBox ID="txtSalesInvoiceDate" runat="server" />

var invDate = $("input[name=txtSalesInvoiceDate]");
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
1

In addition to the answer by @krishna, you can control the web control client ID generation at Page level as well as web site level using ClientIDMode="Static" In ASP.NET 4.0.

At Page level, as shown below :

<%@ Page Title="" Language="C#" MasterPageFile="~/Members/Site.Master" AutoEventWireup="true" CodeBehind="Calculator.aspx.cs" 
Inherits="store.Members.Calculator" 
ClientIDMode="Static"
%>

At web site level

You can use the web.config settings as shown below

 <system.web>
    <pages ClientIDMode="Static">
    </pages>

   ...
 </system.web>
M.Hassan
  • 10,282
  • 5
  • 65
  • 84