C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Useful Tips for Master Page


Posted Date: 23 Nov 2007    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: KamalMember Level: Gold    
Rating: 1 out of 5Points: 10



When we use master page in ASP.Net it will append the Content Placeholder Id with the ID of the contained controls.i.e when there is a textbox with ID “txtAccountNumber” it will be rendered as “ctl00_ContentPlaceHolder1_txtAccountNumber” where “ContentPlaceHolder1” is the ID of the Content Placeholder of the Master Page. This makes us to use the ID “ctl00_ContentPlaceHolder1_txtAccountNumber” whenever we want to access it in JScript. We can access the textbox in Jscript like,

document.getElementById(' ctl00_ContentPlaceHolder1_txtAccountNumber ') ;

But the above scenario will leads to script error if we unknowingly change the ID of the ContentPlaceholder in master Page. So whenever we change the ContentPlaceholder ID we need to revisit all the Jscript code and change the corresponding ID’s. To prevent this rework we can register a hidden control from code behind which in turn will hold the client ID of the control.

Page.RegisterHiddenField("hdnAccountNoTextID", txtAccountNumber.ClientID);

Now we can make use of this hidden variable to get the client ID of the textbox in Jscript.

//gets the Client ID of the txtAccountNumber textbox
var AccountNoTextID = document.getElementById('hdnAccountNoTextID').value;

//Access the txtAccountNumber textbox
var AccountNoText = document.getElementById('hdnAccountNoTextID');

By doing this we can prevent the rework even if someone changes the ContentPlaceholder ID in future our script will be executing without any error.
The same scenario applies when we use a control inside user control.




Responses

Author: Kaushal Solanki    12 Jun 2008Member Level: Bronze   Points : 1
it is really good article but needs more explanation on the same.


Author: muthuvasagan    13 Jun 2008Member Level: Silver   Points : 0
This is very useful information .


Author: Sebastian    13 Jun 2008Member Level: Gold   Points : 1
This is very informative. Thanks for sharing the details.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: ASP.NET 2.0, new features
Previous Resource: Xml based Dynamic Loading of content and Images with Ajax.Net 2.0
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use