Click here to Skip to main content
15,891,529 members
Articles / Web Development / ASP.NET

Master Page, Child Pages: Dynamic Communication Flexibility

Rate me:
Please Sign up or sign in to vote.
4.73/5 (9 votes)
4 May 2009CPOL1 min read 36.1K   53   4
How to have "static" buttons on a Master Page call functions in child pages when clicked.

Introduction

Over on the ASP.NET forums, where I am a moderator, a user asked a question concerning Master Pages. I thought it was an interesting enough question to spend a little time on it and write a BLOG entry (golf season hasn't started in my state yet).

The question was how to have "static" buttons on a Master Page call functions in child pages when clicked. As usual, I wanted the simplest possible way to accomplish the goal.

The first thing to do was create a Master Page with three "Aux" buttons. I used a simple table to keep the buttons at the top of the form and they are all disabled by default. It will be up to the child pages to enable them as needed.

ASP.NET
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" 
    Inherits="MasterPage" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">    
    <title></title>
</head>
<body>   
    <form id="form1" runat="server">   
    <table>      
        <tr><td>         
            <asp:Button ID="_AuxButton1" runat="server" Text="Aux 1" Enabled="false" />
            <asp:Button ID="_AuxButton2" runat="server" Text="Aux 2" Enabled="false" />
            <asp:Button ID="_AuxButton3" runat="server" Text="Aux 3" Enabled="false" />
        </td></tr>      
        <tr><td>          
            <div style="background-color: #00FFFF">              
                <br />
                This is Placeholder1:              
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">   
                </asp:ContentPlaceHolder>             
            </div>       
        </td></tr>   
     </table>   
     </form>
</body>
</html>

Next, we expose the Master Page buttons as properties so the external entities, aka child pages, can have access to them:

C#
public partial class MasterPage : System.Web.UI.MasterPage
{   
    protected void Page_Load(object sender, EventArgs e)    
    {    
    }    

    // expose the Aux buttons    
    public Button AuxButton1    
    {        
        get { return _AuxButton1; }   
    }    
    public Button AuxButton2   
    {       
        get { return _AuxButton2; }  
    }    
    public Button AuxButton3   
    {        
        get { return _AuxButton3; }   
    }
}

That's it for the Master Page. In the child page, we've set the MasterPageFile but there is one more thing we do to make life simpler: We set the MasterType property so the child pages "know about" the type of Master Page:

ASP.NET
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"    
    CodeFile="Child1.aspx.cs" Inherits="Child1" Title="Untitled Page" %> 

<%@ MasterType TypeName="MasterPage" %>

 <asp:Content ID="Content1"
    ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    This is child content<br />  
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />    
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</asp:Content>

Then, in the child page's Page_Load function, we hookup the buttons:

C#
public partial class Child1 : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)    
    {        
        // hook up our events to the master pages buttons        
        Master.AuxButton1.Click += Aux1_Click;        
        Master.AuxButton2.Click += Aux2_Click;         

        // by default, the buttons are disabled, if we are        
        // going to use them, enable them       
        Master.AuxButton1.Enabled = true;       
        Master.AuxButton2.Enabled = true;        

        // custom button text        
        Master.AuxButton2.Text = "Click me!";     
    }    

    // functions called from Master Page   
    protected void Aux1_Click(object sender, EventArgs e)  
    {       
        Label1.Text = "Aux1Clicked!";   
    }   
    protected void Aux2_Click(object sender, EventArgs e)   
    {       
        Label2.Text = "Aux2 has been clicked!";   
    }
}

Here is what the browser looks like after we've clicked the aux buttons:

  • Aux button 3 remains disabled because we did not connect it to any function.
  • Aux button 2 has custom text we applied.
  • The two Child Page label controls have been updated from the Master Page.

I hope someone finds this useful.

Steve Wellens

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

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


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
GeneralQuite clear Pin
Gildor_haohao11-May-09 19:16
Gildor_haohao11-May-09 19:16 
GeneralThanks! Pin
frank3si5-May-09 4:16
frank3si5-May-09 4:16 
Generalvery nice Pin
Donsw3-May-09 11:18
Donsw3-May-09 11:18 
GeneralI like it. Pin
lviolette6-Apr-09 19:22
lviolette6-Apr-09 19:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.