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" %>
3:
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;
5:
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: }
23:
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" %>
3:
4: <%@ MasterType TypeName="ThreePLogic.Web.ASPWeb.ContactPages.ContactMasterPage" %>
5:
6: <asp:Content ID="Content1" ContentPlaceHolderID="GridContent" runat="server">
7:
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;
6:
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!