Translate

Tuesday, March 22, 2011

Get reference to master page in asp.net


In this example below assume there is a label with ID= lblMessage in the Master page. Now if you want to access that control in the child page and set its value as "Hello World", this is how you do it.

(Ive used some colors down there so that you can quickly track the child page contents back to the master page.)


Master page code

namespace BugTracker.Master
{

public partial class BTMasterPage : System.Web.UI.MasterPage
{

}

}


Child page code

using BugTracker.Master;

namespace BugHistory
{


     public partial class ChildPage: System.Web.UI.Page
      {

            protected void Page_Load(object sender, System.EventArgs e)
           {
               private BTMasterPage PageMaster  = (BTMasterPage)Page.Master;
               lblMessage = (Label)PageMaster.FindControl("lblMessage");
               lblMessage.Text="Hello World";
            }
      }
}

Further Reading:

How to: Reference ASP.NET Master Page Content
http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx





No comments:

Post a Comment

Comments will appear once they have been approved by the moderator