How to Access ASP.NET Controls client id in JQuery


You are using JQuery with ASP.NET and you want to access client ids of ASP.NET controls in JQuery.

When ASP.NET renders server side controls it gives them unique client ids which are different from their server ids and if you check view source of your ASP.NET page you will find ids familiar to following.

ctl00_ContentPlaceHolder1_txtName

This id can be even more lengthy if you are using Master pages in your ASP.NET website. So how you can access these controls in JQuery. The quick workarround is to use following technique.

$("#'<%=txtName.ClientID %>'");

Another approach which enables you to avoid server tags completely is to match the elements by the end part of their names like following example.

$("[id$='_txtName']");








 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments 1

user
Utariq
  • 21 July 2011

Second approach is cool :)

Add Comment