How to access the server control of content page inside the javascript?

Access the server control of content page inside the javascript

Sometime i used to create websites using Master Page. Some server controls are added into content page. For some cases i need to use to change the value of those control at client side. So usually write javascript code.

For example :

Suppose i want to add two number and show the result. I want to perform addition in client side and show the result on content page's Label control.

for this first i wrote the below code :

function addNumber(val1,val2)
var result=val1+val2;
document.getElementById('Label1').innerHTML = result;

But the above code gives javascript error. i.e : unrecognised id.
Because the content page's control id converted as per contentplace holder id.
So i modified the code to below code.

function addNumber(val1,val2)


var result=val1+val2;

document.getElementById('<%=Label1.ClientID%>').innerHTML = result;


The above code works fine.

so whenever you want to access the content page's control at client side just put the clientID of that control not the control id only.


Article by Manoranjan Sahoo
If it helps you then Rate this. Best Regards, Manoranjan Sahoo Blog:

Follow Manoranjan Sahoo or read 63 articles authored by Manoranjan Sahoo


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: