Click here to Skip to main content
15,885,309 members
Articles / Web Development / HTML

Interaction Between Content Page and Master Page

Rate me:
Please Sign up or sign in to vote.
4.94/5 (15 votes)
30 Apr 2009CPOL5 min read 158.9K   3.5K   58  
Discussion on the concept and implementation of interaction between a content page and a master page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ContentPage : System.Web.UI.Page
{
    //raise button click events on content page for the buttons on master page
    protected void Page_Init(object sender, EventArgs e)
    {
        Master.PropertyMasterButton1.Click += new EventHandler(PropertyMasterButton1_Click);
        Master.PropertyMasterButton2.Click += new EventHandler(PropertyMasterButton2_Click);
    }

    //Send text from Master Box 1 to Content Box 1
    protected void PropertyMasterButton1_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.GetMasterbox1Value();

        //option 3. use FindControl() method of Master object
        txtContentBox1.Text = ((TextBox)Master.FindControl("txtMasterBox1")).Text;
    }
    //Get text from Content Box 2 to Master Box 2
    protected void PropertyMasterButton2_Click(object sender, EventArgs e)
    {
        //use one of the options below
        //option 1. User master page's property
        Master.PropertyMasterTextBox2.Text = txtContentBox2.Text;

        //option 2. call master page's method
        Master.SetMasterBox2Value(txtContentBox2.Text);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    //send text from Content Box 1 to Master Box 1
    protected void btnContentButton1_Click(object sender, EventArgs e)
    {
        //use one of the options below
        //option 1. use master page public property
        Master.PropertyMasterTextBox1.Text = txtContentBox1.Text;

        //option 2. alternativly, call Master page public method to assign a value to the control on master page
        Master.SetMasterBox1Value(txtContentBox1.Text);
    }
    //Get text from Master Box 2 and display it in Content Box 2 
    protected void btnContentButton2_Click(object sender, EventArgs e)
    {
        //use one of the options below
        //option 1. access master page's TextBox through a public property exposed on mater page
        txtContentBox2.Text = Master.PropertyMasterTextBox2.Text;

        //option 2. Call master page public method
        txtContentBox2.Text = Master.GetMasterBox2Value();

        //option 3. use FindControl() method of Master object
        txtContentBox2.Text = ((TextBox)Master.FindControl("txtMasterBox2")).Text;
    }

    
 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Web & Database Developer. Design and implement web and database applications utilizing Microsoft and other development tools.

Comments and Discussions