Hikari
Hikari

Reputation: 3937

Page call its masterpage's properties and public methods

There are some data like title, headers, etc, that are in masterpage. I wanna set these data based on each page that uses masterpage.

For that, I created some properties like public string title {get; set;}, and set default values in masterpages's PageLoad().

But, how can I access these properties? As I understand, masterpage is a class that's instantiated for each pageload, so I'd need to find its object to call its properties.

How can I do that?

Upvotes: 0

Views: 122

Answers (2)

baaroz
baaroz

Reputation: 19587

you should put this code in the design view(in html)

<%@ MasterType VirtualPath="~/NameOfMasterPage.Master" %>

under the

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeBehind="default.aspx.cs" Inherits="MOCKUPPROJECT._Default" EnableEventValidation="false" %>

Now you can change it properties

Master.MasterContentHolderPage="some values"

Upvotes: 1

Ryan M
Ryan M

Reputation: 2112

Page.Master refers to the current page's MasterPage. You can cast this to the relevant type and access the property that way. In the master page:

<%@ Master Language="C#" ClassName="MasterExample" %>

<script runat="server">
    public string SiteName
    {
        get { return "My Site Name"; }
    }
</script>

Then, in the page:

protected void Page_Load(object sender, EventArgs e)
{
  MasterExample m = (MasterExample)Page.Master;
  mylabel.Text = m.SiteName;
}

See the bottom of http://msdn.microsoft.com/en-us/library/system.web.ui.masterpage(v=vs.110).aspx.

Upvotes: 3

Related Questions