Remove Name Mangling from ASP.Net Master Pages (get your ID back!)

If you have been programming using ASP.Net Master Pages and javascript then you have no doubt come across the problem where you try to access an element in javascript using:

var sValue = document.getElementById(’TextBox1′).value;

And you keep getting an error that the element cannot be found. If you check the HTML source for the page, you see that has “helpfully” added something like ‘ctl00_ContentPlaceHolder1_’ to the front of the control ID, so you have to use:

var sValue = document.getElementById(’ctl00_ContentPlaceHolder1_TextBox1′).value;

I’m not going to try & sugar coat it - this totally sucks.

The solution that I have been using overwrites the .getElementById method, allowing you to search for the element just using the normal ID.

<script type='text/javascript'>
document.newGetElementById = document.getElementById;
document.getElementById = function(sElementID)
var oFirstTry;

oFirstTry = document.newGetElementById(sElementID);
if (oFirstTry)
	return oFirstTry;
	return document.newGetElementById('ctl00_ContentPlaceHolder1_' + sElementID);

Thanks to Scooby Doo for pointing out the earlier error, it has now been corrected

Add this code to the top of your page or .js file, and then you can go back to using:

var sValue = document.getElementById(’TextBox1′).value;

without caring if it is in a content page or not.

Note: The above code won’t work if you change the name of your ContentPlaceHolder control from the standard ‘ContentPlaceHolder1′. If you do like to have unique names then change

return document.getElementById(’ctl00_ContentPlaceHolder1_’ + sElementID);


return document.newGetElementById(’<%=me.Form.Controls(1).ClientID %><%=Me.ClientIDSeparator %>’ + sElementID);

The code must now always be placed in your page (not an external .js file). However, calls to .getElementById can still be in external .js files.

Finally: A common solution to the problem is to use the ClientID property:

var sValue = document.getElementById(’<% =TextBox1.ClientID %>’).value;

but this method requires that all calls to getElementById be in the page, which is not as flexible as the solution given.

This post brought to you by - Over 10,000 Aged domains for sale!

Related posts:

  1. Remove HTML tags and Javascript from pages

Add GeoLocation to your site in less time than it takes to Drink a Beer

This is what a regular programmer looks like
Adding Geolocation to your website is so easy these days that I’ve decided that not only can we do it within 5 minutes, but to make it interesting I’ll bet that you’ve finished it before you can finish a beer.

OK? Got your beer? Lets go.

  1. Crack open the beer and start a new project. Call it ‘GEOTest’. Take a well deserved drink.
  2. Download the MaxMind Geo database and wrapper code from MaxMind (Complete list of supported languages - thanks again to MaxMind for doing all the work). Have some more beer while it downloads (thanks to my spankinglyfast ADSL2+ connection, no beer for me).
  3. Open the zip file into your project directory. Another sip. Add the files   ‘testCountryLookUp.aspx’, ‘testCountryLookUp.aspx.vb’ and ‘CountryLookup.vb’ to your project.
  4. Thirsty? More beer.
  5. Set the page ‘testCountryLookUp.aspx’ as your startup page, and then run the project. At this point it should run, but you may get a ‘Parser Error’ in the ‘testCountryLookUp.aspx’ file. To fix it, change:
    in the   ‘testCountryLookUp.aspx’ file.
  6. Thats it! Done! You’ve probably still got plenty of beer left, so lets make some of our own code to get the country from IP Address.

Function CountryFromIP(ByVal Address As String) As String
     Dim oCountryLookup As CountryLookup = New CountryLookup(Server.MapPath("data/GeoIP.dat"))

     CountryFromIP = oCountryLookup.LookupCountryName(Address)
End Function

To call it, just use:
CountryName = CountryFromIP(Request.UserHostAddress)
Doesn’t work? Maybe too much beer…Passing your UserHostAddress won’t work from your local machine. To test, grab your IP address from somewhere like

Still doesn’t work? Definitely time for another beer…

This post brought to you by - Over 10,000 Aged domains for sale!

No related posts.