ASP.Net and getElementById()

Prototype has numerous gems including the lovely $ utility function.  I needed to write some custom client side javascript in a small ASP.net app and was looking forward to using this functionality.  However, if you use master pages or custom controls, you're hosed when it comes to client side element ids.  As Aaron and Jacob mentioned in Scott Hanselman's most recent podcast, ASP.Net will generate lovely element ids like ctl00_main_txtContactPerson, and that's only with a simple master page and content form.  When you throw custom controls into the mix, you end up with ctl100_main_grid1_row1_ctl100_txtContactPerson.  Makes even the simplest client side javascript a pain.  I tried to find a similar $ function that was compatible with ASP.Net but didn't have any luck.  So, I wrote my own little utility function to do the trick.

function $n(element)
{
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($n(arguments[i]));
    return elements;
  }
  if (typeof element == 'string') {
    var el = document.getElementById(element);
    if( el ) 
      return el;
  }
  else
    return element;
 
  var regExp = new RegExp('_' + element + '$');
  var formsLength = document.forms.length;
  for( var i=0; i<formsLength; i++ ) {
    var form = document.forms[i];
    var elementsLength = form.elements.length;
    for( var j=0; j<elementsLength; j++ ) {
      element = form.elements[j];
      if( element.id && element.id.match(regExp) ) 
        return element;
    }
  }
 
  return null;
}

$n is identical to $ in design and purpose.  You pass it the name of the asp.net id of the element you are looking for and it searches through your html doc trying to match any element with an id that ends with '_<asp.net id>'.  I realize this isn't fool proof and will fail in the case where you have a txtName on a page and a txtName in a nested user control.  This function will stand on its own (Prototype is not required).  If you want full integration with Prototype, simply replace return element/el with return Element.extend(element/el).  Tested with FF2 and IE6.

posted @ Sunday, March 18, 2007 10:45 AM


Print

Comments on this entry:

# re: ASP.Net and getElementById()

Left by Thomas Einwaller at 7/24/2007 3:05 PM

Hi Bill, thanks for that nice function. I am a fan of Prototype and I do not like ASP.NET that much ;-)

# re: ASP.Net and getElementById()

Left by Anviti Shastri at 8/10/2007 12:41 PM

This one is a help for sure! Thanks.

# re: ASP.Net and getElementById()

Left by Lexxis at 8/29/2007 1:58 AM

Register your own WordPress blog here!

# re: ASP.Net and getElementById()

Left by Rene Drescher-Hackel at 4/5/2008 5:50 AM

[transalte by google ;-)]
Similarly, a collection of elements to create and JSON with a client and thus create object on the client directly. I had times here (http://www.drescher-hackel.de/default,date,2007-07-22.aspx). The solution proposed here is also quite cool. A disadvantage, however, is that any request for an item on the entire collection on the client and more.

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 7 and type the answer here:
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789