Skip to content
April 23, 2011 / Manoranjan

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

Hello,
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.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.