Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Monday, July 30, 2012

Access Master Page controls from the Content Page in Asp.Net

In this article we will discuss how we can access Master page control values in Content pages in Asp.Net. If you are interested in SharePoint then you can read an article on Master Pages in SharePoint 2010.

Master page has lots of benefits specially if you more than one page needs same looks and feels and functionality in a web site. A very simple example of usage of Master page is for Header and Footer. Normally almost all the pages contains Header and Footer. So it is always good to put that in master page.

Suppose your master page contains some content in a Textbox and You want to retrieve the values in a content page, then you can archive this using 2 ways.

First Approach:
Define one property in the master page like below:

public string ValueOfTextBox
{
    get
    {
        return txtMasterTextBox.Text;
    }

    set
    {
        txtMasterTextBox.Text = value;
    }
}
Now in the content page write the directive statement like below:

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

MasterPage.master is the name of the master page.

Not to retrieve the value like below:
Set the value:
Master.ValueOfTextBox = "Write what ever you want";

Get the value:
String text = Master.ValueOfTextBox ;

Second Approach:
In this approach write the code in the content page like below:

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

MasterPage.master is the name of the master page.

Now retrieve the values like below:

TextBox textBox = (TextBox)Master.FindControl("txtMasterTextBox");
textBox.Text = "Write what ever you want";

Here txtMasterTextBox is the name of the textbox presented in the master page and whose value we want to get or set.




0 on: "Access Master Page controls from the Content Page in Asp.Net"