jQuery ID Selector for ASP.NET Controls

by Arra Derderian 27. October 2010 00:23

jquery

Having to select .NET controls via jQuery is easy because you can specify a unique class name for whatever control you want to select and use the "." selector to find that element on the client side. The problem is the class selector is slower than the id selector and it also breaks the rules of separating functionality and design. You end up with class names that may get stripped out by a CSSS developer later on. You also have the extra markup required on every element for this class name.

Hoping to find some way to select elements based on .NET id I Googled around and found the following code which adds a custom selector to the jQuery ":" selector called "asp".

Solution 1

jQuery.expr[':'].asp = function (elem, i, match) { return (elem.id && elem.id.match(match[3] + "$")); };

Here we return any elements that have an ID and secondly, end with the id string specified. You would call this code like this:

Calling It

$(":asp(SelectTextBox)")

After looking at this I noticed that because we are using the "EndsWith" regex match that we will get any control with an ID that ends with "SelectTextBox". This is a problem because if we have a control called "NewSelectTextBox" and then another called "SelectTextBox" they both will be selected. So I looked at the .NET markup generated from these two controls existing on the same page and saw that an underscore("_") was preceding the final controls .NET ID on the client. I then came up with solution #2.

Solution 2

jQuery.expr[':'].asp = function (elem, i, match) { return (elem.id && elem.id.match("_" + match[3] + "$")); };

Seems like an easy fix. This would eliminate any similarly named controls. Well there is one last problem that I am still working on getting around. That is that if you nest one of these textboxes in a custom control for example, it will print out the textbox id ending with the same "_ID" and add the parent controls name to the front of it. If there is two of these custom controls on a page you will have to unique controls on the page, but they will both end with "_SameName". I am still wondering how to solve this issue. Perhaps we add more logic to the custom selector to only return the first found element? What do people think? I mean in these situations you would probably want multiple controls returned anyways because you would be modifying all the items or you could combine the selector with a ":first" if you wanted. You probably would end up generating some sort of extra markup for uniqueness as well because the items will most likely represent an ID in your database. Anyways, I thought I would share this and see if anyone had any ideas on it.

Tags: , , , ,

ASP.NET | Engineering | jquery | Technology

Comments

9/27/2011 12:36:14 PM #

filter elements

the blog post is really the freshest on this noteworthy theme. I harmonize with your conclusions and will thirstily look forward to hear your coming updates.

filter elements United States | Reply

11/7/2011 12:06:19 PM #

Marrakech guide

Merely wanna input that you enjoy a same clean locale, I charity the intent furthermore fad it really stands external.

Marrakech guide France | Reply

11/13/2011 11:35:03 AM #

Tout Marrakech

Deference to condition founder , several grand choosy material . “When you desist to ideal you desist to reside.” by Malcolm Stevenson Forbes.

Tout Marrakech France | Reply

11/16/2011 5:24:26 AM #

maroc immobiliers crise

Defunct pent columns , esteem it for entropy.

maroc immobiliers crise France | Reply

11/17/2011 1:23:27 PM #

bmw scanner

which she acquired as a brutal(ly good-looking) member of the WCW and WWE circuit

bmw scanner Oman | Reply

12/14/2011 8:13:21 AM #

New Era snapback


SO Luky can reay this  article and  inspire by your iders<a href="http://www.nflsnapbacksale.org/">Snapbacks Hats</a> , The topic is a fresh take on a few things. the works <a href="www.nflsnapbacksale.org/nfl-snapback-hats-c-272.html">Snapback Nfl  Hats</a>to the layout of a balanced,innovative themes, delicate lace, special the article of this core  value  told us we should read and prove anything we want, for we<a href="http://www.nflsnapbacksale.org/">New Era snapback</a> are sufficientto judge aright  

New Era snapback People's Republic of China | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading