Access ASP.NET Master Page controls from Content Page

Mentioned here are two ways you can access MasterPage controls from Content Page

Approach 1

Go to the Content page (Default.aspx) and add the following line below the <Page> directive to register the MasterPage


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




Now in the code behind of ContentPage, access the MasterPage control in the following manner

C#


TextBox tb = (TextBox)Master.FindControl("txtMaster");


tb.Text = "Something";




VB.NET


        Dim tb As TextBox = CType(Master.FindControl("txtMaster"), TextBox)


        tb.Text = "Something"





Approach 2

Expose the Text property of the TextBox control in the MasterPage and use get set properties as shown below

C#


public string SomeText


{


    get


    {


        return txtMaster.Text;


    }


    set


    {


        txtMaster.Text = value;


    }


 


}





VB.NET


    Public Property SomeText() As String


        Get


            Return txtMaster.Text


        End Get


        Set(ByVal value As String)


            txtMaster.Text = value


        End Set


 


    End Property




Now accessing the Text property from the Content page is as simple as this line of code. Add the code in the codebehind of the MasterPage


Master.SomeText = "Something";







About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

6 comments:

Enel Almonte said...

I Tried to do the Aproach 2 in Visual studio 2010 and it did not worket.

How it works in 2010?

Anonymous said...

Approach 2 also requires the line:

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

at the top of your content page.

Anonymous said...

not work, the property or control is good but not transfier de value
from masterpage to contentpage.

Anonymous said...

Code From MasterPage:
Protected Sub btnSearch_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click
Try
'txtFiltros.Text
Me.FilterColumn = Me.cboFilter.SelectedValue
Me.FilterValue = Me.txtFilter.Text
Response.Redirect("~/modApp/frmSquareView.aspx")

Code From ContentPage(frmSquareView.aspx):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not IsPostBack Then
Me.FilterColumn = Me.Master.FilterColumn
Me.FilterValue = Me.Master.FilterValue

Anonymous said...

send value from ContentPage to MasterPage is correct.
CODE:
Me.Master.ptyFilterValue = "TEST"

Unknown said...

I have tried Approach 2, and it worked..
However,
I used some javascript at the master page, and manage to change the text value at the master page.. (The screen showed that the value at the master page changed)
When I access the value from the content page using approach 2, the value was still the original value. (meaning that it did not change)..