ASP.NET ClientID & jQuery small trick

Up to ASP.NET 3.5 the client ids for .NET controls is not for passionate readers it looks like this: “ctl00_ctl00_decoratedArea_contentArea_homeSearch_mileageDropDownRow”
On the other hand the client name is “ctl00$ctl00$decoratedArea$contentArea$homeSearch$mileageDropDownRow” so this does not really help.

If you have to reference this from client code:
1) Migrate to 4.0 ( best option )
2) Use <%controlName.ClientID %>, but this only applies in a aspx or ascx file

If you are writing code in a js file there is no easy way, but the jquery to the resque!
Just use selector like this $(‘id$=shortServerId’)
This selects all elements which have attribute id ending with ‘shortServerId’ and returns them as array. If you want to apply non-jquery code to your selection as normal DOM functions first reference to array element of choice like this.

var allElements = $('id$=shortServerId');
if(allElements.length > 0) {

Needless to say it is a lot better to make yourself a favor and use only jQuery for  DOM modifications.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: