Saturday, March 27, 2010

How To Access Control On Master Page From Content Page

It a common requirement that you have change the master page content from content page. For example you have welcome label control that is there in master page and you want to change the welcome message after user logged in from the content page. Same way there might be another requirement like you need to bind a menu from content page as per user logged in having rights. In such kind of case use find control() method to access control on master page from the content page.You can find any control of master page like grid view,repeater and other controls. Let’s take above scenario where you have a label called lblWelcome in your master page and you need to change welcome message from content so first we have to create a label in master page like following.

<asp:Label ID="lblWelcome" runat="server"></asp:Label> 
Then with the help of find control method you have to first find the control with id and then you have to typecast that control as label and then you can do same thing as you can do with label.Here our requirement is changing welcome message so we are changing text of it.Like following.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label lblWelcome = Page.Master.FindControl("lblWelcome")
as Label;
lblWelcome.Text = "Welome message from content page";
}

}
So that’s it you can easily access master page control from the content page. Hope this will help you

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
Loading...
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews