Find control inside GridView using jQuery

In this post, find jQuery code and explanation to "find control inside ASP.NET GridView". Why it is tricky? The reason is ID of control's placed inside ASP.NET Gridview get changed at the time of rendering. Till ASP.NET 3.5, the rendered client-side id is formed by taking the Web control's ID property and prefixed it with the ID properties of its naming containers.

In short, a Web control with an ID of txtFirstName can get rendered into an HTML element with a client-side id like "ctl00_MainContent_txtFirstName". And same is true for ASP.NET GridView.

Suppose, there is textbox and label control in gridview.
   <asp:TextBox ID="txtID" runat="server" />
   <asp:Label ID="lblID" runat="server" />
And when you run this, this is rendered as below.
<input name="gdRows$ctl02$txtID" type="text" id="gdRows_ctl02_txtID" />
<span id="gdRows_ctl02_lblID"></span>
As you notice, the ID of the control is changed. It is no more "txtID" or "lblID". Gridview ID and control number is added as prefix. So now how do you select these controls?

Related Post: To select all label which has ID as "lblID".
$('#<%=gdRows.ClientID %>').find('span[id$="lblID"]').text('Your text.');
To select particular label control out of all the labels which ends with "lblID".
var $arrL = $('#<%=gdRows.ClientID %>').find('span[id$="lblID"]');
var $lbl = $arrL[0];
$($lbl).text('Your text...');
Similarly, you can also find textbox. To select all textbox which has ID as "txtID".
$('#<%=gdRows.ClientID %>').find('input:text[id$="txtID"]').val('Your value.');
To select particular textbox control out of all the textboxes which ends with "lblID".
var $arrT = $('#<%=gdRows.ClientID %>').find('input:text[id$="txtID"]');
var $txt = $arrT[0];
Feel free to contact me for any help related to jQuery, I will gladly help you.

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..