How To Access Content From MasterPage in ASP.NET

So, you want to access some content from inside a page that you created from an existing master page?  The most clean way to do this is to create a public property in your master page, then access that.  In my case, I have a search button on a master page that I want to use from inside to pages that derive from that page.  So, Here is how I declare my master page:

   1: <%@ Master Language="C#" MasterPageFile="~/MasterPages/MasterPage.Master" AutoEventWireup="true"
   2:     CodeBehind="ContactMasterPage.master.cs" Inherits="ThreePLogic.Web.ASPWeb.ContactPages.ContactMasterPage" %>
   4: <asp:Content ID="Content5" ContentPlaceHolderID="ContentCenter" runat="server">
   5:         <asp:TextBox ID="TextBoxSearchName" runat="server" Width="200px" CssClass="InputText"></asp:TextBox>
   6:         <asp:Button ID="ButtonSearchName" runat="server" CssClass="SearchBtn" />
   7:         </div>
   8: </div>

In my codebind for my masterpage, I have the following:

   1: using System;
   2: using System.Web.UI;
   3: using System.Web.UI.WebControls;
   4: using ThreePLogic.Data.ThreePLogicAccess;
   6: namespace ThreePLogic.Web.ASPWeb.ContactPages
   7: {
   8:     public partial class ContactMasterPage : MasterPage
   9:     {
  10:         public string SearchText
  11:         {
  12:             get
  13:             {
  14:                 TextBox textBoxSearch = Master.FindControl("ContentCenter").FindControl("TextBoxSearchName") as TextBox;
  15:                 if (textBoxSearch != null)
  16:                 {
  17:                     return textBoxSearch.Text;
  18:                 }
  19:                 else
  20:                 {
  21:                     return String.Empty;
  22:                 }
  24:             }
  25:         }


Then, in my page I derive from the masterpage I have the following.  Notice that I include the masterpage type so I can have type safe access to the public property.


   1: <%@ Master Language="C#" MasterPageFile="~/ContactPages/ContactMasterPage.Master"
   2:     AutoEventWireup="true" CodeBehind="ContactCompanyMasterPage.master.cs" Inherits="ThreePLogic.Web.ASPWeb.ContactPages.ContactCompany.ContactCompanyMasterPage" %>
   4: <%@ MasterType TypeName="ThreePLogic.Web.ASPWeb.ContactPages.ContactMasterPage" %>
   6: <asp:Content ID="Content1" ContentPlaceHolderID="GridContent" runat="server">
   8:     <asp:Label ID="LabelSearchMirror" runat="server" Text=""   Visible="false" ></asp:Label>


Then, finally, in the codebehind, this is how I access my search box.  In my case, I assign it to an invisible label, then I can have server side presentation controls, like GridView, access it as a select parameter.

   1: using System;
   2: using System.Web.UI;
   3: using System.Web.UI.WebControls;
   4: using ThreePLogic.Common.Enums;
   5: using ThreePLogic.Web.ASPWeb.Code;
   7: namespace ThreePLogic.Web.ASPWeb.ContactPages.ContactCompany
   8: {
   9:     public partial class ContactCompanyMasterPage : MasterPage
  10:     {
  11:         protected void Page_PreRender(object sender, EventArgs e)
  12:         {
  13:             GridViewCompany.PageIndex = UserService.I.GetCookieSelectedId(CookieIdType.CompanyCurrentPage);
  14:             if (!String.IsNullOrEmpty(Master.SearchText))
  15:             {
  16:                 LabelSearchMirror.Text = Master.SearchText;
  17:                 GridViewCompany.DataBind();
  18:             }
  19:         }

Good luck, and hope this helps!

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he’s a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


  1. BadGirl35 says:

    Does your relationship with your work change after it is published and if so, how? ,

  2. Oh my gosh, better be careful giving out advice like this. (Heavy sarcasm noted.)

    Check out a post I made on the ASP.NET forums back in 2006, and the wrath it incurred from some know-it-all, monolythic coders:

    Thanks for posting this. It a technique I’ve been using successfully for years on a very big site. It simplifies a great many things.



Get every new post delivered to your Inbox

Join other followers: