Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 148 - Passing data from content page to master page in asp.net

Suggested Videos
Part 145 - Implementing autocomplete textbox in asp.net web forms
Part 146 - Why use master pages in asp.net
Part 147 - Master pages in asp.net



This is continuation to Part 147, please watch Part 147 before proceeding.

Let us understand how to pass data from a content page to a master page, with an example. 

In the example below,
1. We have a textbox in the master page. The ID of the textbox is txtBoxOnMasterPage

2. On the Content page, we have another textbox and a Button. The ID of this textbox is TextBox1 and the ID of the button is Button1

3. When you type some text in the textbox on the content page and when you hit Set Text button, we want to display the text entered in the textbox on the content page in the textbox that is present in the master page.



Here are the steps:
Step 1: Place a TextBox control with ID="txtBoxOnMasterPage" on the master page. Here is the HTML
Content Page Data:<br />
<asp:TextBox ID="txtBoxOnMasterPage" runat="server"></asp:TextBox>

Step 2: In the code-behind file of the master page, include a public property that returns the textbox control. Content pages will use this property to set the text of the textbox on the master page. 
// The property returns a TextBox
public TextBox TextBoxOnMasterPage
{
    get
    {
        // Return the textbox on the master page
        return this.txtBoxOnMasterPage;
    }
}

Step 3: Include a TextBox and a Button control on the content page. Here is the HTML.
<b>Type the text in the textbox that you want to display 
in Master Page TextBox and click Set Text Button</b>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Set Text" 
onclick="Button1_Click" />

Step 4: Copy and paste the following Button1_Click() event handler method in the content page
protected void Button1_Click(object sender, EventArgs e)
{
    // Retrieve the master page associated with this content page using
    // Master property and typecast to the actual master page and then
    // reference the property
    ((Site)Master).TextBoxOnMasterPage.Text = TextBox1.Text;
}

If you want Master property to return a strongly typed reference of the associated master page, include the following MasterType directive on the content page.
<%@ MasterType VirtualPath="~/Site.Master" %>

Once you have included the above MasterType directive on the content page, you will now have a strongly typed reference of the master page and can access it's properties without having to typecast.
protected void Button1_Click(object sender, EventArgs e)
{
    Master.TextBoxOnMasterPage.Text = TextBox1.Text;
}

To retrieve a master page associated with a content page, we can use either
1. Master propery
2. Page.Master property

Page.Master property always returns an object of type MasterPage and we need to explicitly typecast it to the actual master page, if we need to access it's properties and methods.

Where as Master property returns a strongly typed reference of the actual master page if the content page has MasterType directive specified. Otherwise Master property works in the same way as Page.Master property.


No comments:

Post a Comment

It would be great if you can help share these free resources