Pages

Tuesday, January 3, 2012

How to access content page controls from master page and vice-versa

0 comments
Using FindControl method :

Access master page control from content page :

protected void Page_Load(object sender, EventArgs e)
{
   
//accessing master page control from content page.
   txtContentBox1.Text = (this.Master.FindControl("txtMasterBox1") as TextBox).Text;

Accessing content page controls from master page:

protected void Button1_Click(object sender, EventArgs e)
{
   
//accessing content page control from master page.
        ((TextBox)ContentPlaceHolder1.FindControl("txtContentBox1")).Text = "AnyText";


Using Properties and Event handlers:-

To interact with the Master page, first place a MasterType directive at the beginning of a content page to establish a communication link between the content page and its master page:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

MasterPage.master is the file name of the master page. This directive enables a “Master object” on the content page to provide a programming interface for the interaction with the MasterPage, so that you can access the properties defined in master page using Master keyword.

Using properties and methods:

Expose the server controls on the master page by means of public properties and methods so that they can be accessed from content page.
The following code sample creates public properties for the Textbox and Button on the master page and also exposes methods that assign values or retrieve values for the Textbox.

//expose controls on master page as public properties
public TextBox PropertyMasterTextBox1
{
    get { return txtMasterBox1; }
    set { txtMasterBox1 = value; }
}
public Button PropertyMasterButton1
{
    get { return btnMasterButton1; }
}

//expose public methods on master page for content page to call
public void SetMasterTextBox1Value(string myText)
{
    txtMasterBox1.Text = myText;
}
public string GetMasterTextbox1Value()
{
    return txtMasterBox1.Text;
}

On the content page, the above properties and methods can be accessed through Master object:-

//Send text from Contentpage TextBox to Masterpage TextBox

        //use one of the options below
        //option 1. use master page public property
        Master. PropertyMasterTextBox1.Text = txtContentBox1.Text;

        //option 2. Call Master page public method to assign a value to the control on master page
        Master.SetMasterTextBox1Value(txtContentBox1.Text);


Using Event handlers:

You can access the button control which is exposed as a public property in master page from content page and can subscribe for its click event in content page.

Event should be subscribed in the Page_Init event of content page. This is because the events have to be raised on every postback. It is much cleaner to leave the code here than in Page_Load in order to avoid the complexity dealing with the PostBack() logic in Page_Load.


//subscribe for button click event on content page for the button on master page
protected void Page_Init(object sender, EventArgs e)
{
    Master. PropertyMasterButton1.Click += new EventHandler(MasterButton1_Click);

}

//Send text from Masterpage TextBox to Contentpage TextBox
protected void MasterButton1_Click(object sender, EventArgs e)
{
        //use one of the options below
        //option 1. use master page public property

      txtContentBox1.Text = Master.PropertyMasterTextBox1.Text;


          //option 2. Call master page method
        txtContentBox1.Text = Master. GetMasterTextbox1Value();

}

Hence you can use different ways to interact with master page from content page and vice-versa according to the requirements.

Leave a Reply