To get around the server-side-id vs client-side-id differences you can
often use id selectors like this $("DIV[id$=myServerSideID]") but
there is always the possibility of confusion between elements whose
IDs end with the same pattern of letters.

For an internal project we've used a couple of quick and dirty custom
selectors called :aspid() and :aspname()   The :aspid() selector
suffers from the same issues as the "$id=..." solution above.
The :aspname() selector will not get confused between elements but it
only works on form elements because they have a name attribute.

They rely on the way asp.net build client IDs and Names that end with
the server-side id...
I would not recommend widespread use of these selectors because they
are so asp-specific but they can be helpful none the less.

Cheer. I hope this helps a wee bit.

jQuery.extend(jQuery.expr[':'], {

        /**
         * Custom selector for matching elements by their server-side ID
(using their client-side IDs generated by ASP.Net)
         * They may return multiple elements when more than one client-side
id ends with the same id string.
         *
         * (Note: The :aspid() selector matches on the element id.
The :aspname() selector matches on the element name (so it only works
on form elements that support the name attribute). The latter is more
accurate because it can rely on the "$" separator in the name
attribute to correctly locate the start of the id in the string)
         *
         * @param String id The ID to search for. This is the id you used in
the ASP server-side code.
         *
         * @example $("INPUT:aspid(buttonEdit)")
         * @desc Find the input element who's server-side id is "buttonEdit".
(The client-side id will be something like
ctl00_MainContent_buttonEdit)
         *
         * @name :aspid, :aspname
         * @type jQuery object
         *
         * @cat jQuery/Custom selector
         * @author George Adamson
         */
        "aspid"         : function(a,i,m){ var id=a.getAttribute("id");   
return id
&& m[3] && id.substr(id.length - m[3].length - 1) == "_"+m[3]; },
        "aspname"       : function(a,i,m){ var id=a.getAttribute("name"); 
return id
&& m[3] && id.substr(id.length - m[3].length - 1) == "$"+m[3]; }
});

Reply via email to