from last few days we are working with Master Page and Java script and as the all Javascript do it gets the control by ID using “document.getElementByID(<ID OF CONTROL>).value”..That’s it.it looks simple but frankly speaking it has wasted our so much time…..Looks funny na???so let me tell you why??
defn [Name Mangling wikipedia]: In software compiler engineering, name mangling (more properly called name decoration, although this term is less commonly used) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
Problem:
you can do it simply by document.getElementByID..but you will get stuck when your control lies under Master Page…if you keep the same function which you have used for simple page[without Master page] IE Will say “Object required”. Because in your simple page the ID of control will be same…and if you add Master Page than id will be something like ctl00_ContentPlaceHolder1_txtName.. let’s see it by example
Suppose you have one control in simple ASP.NET Page and has ID txtName.
Now if you put it in MasterPage which has ContentPlaceHolder and had ID= ContentPlaceHolder1.
so now your ID= ctl00_ContentPlaceHolder1_txtName So the pattern for generating name i like :
ctl00_<ContainerID>_<CONTROL ID> for example ID = ctl00_ContentPlaceHolder1_txtName
Hope you are clear now.. so how you will find you control by this ID????
Solution:
in old Javascript you will refer control like:
document.getElementById(“txtName”).value
and Now you have to do:
document.getElementById(“<%= txtName.ClientID %>”).value
That’s it.
Looks cool na??
Hope you have enjoyed this article.
Happy Java scripting.
Kiran Patil
UPDATED 19-2-2008 VER 1.1
I have tried this with my page which has .js file located under different folder and i have linked it with page using tag.
But it was not working because Visual studio doesn’t Parse thisĀ file and ClientID will not work.
So, Conclusion is thatĀ you can’t use ClientID with different .js file