将属性从母版页传递到aspx页

时间:2022-10-30 10:11:54

I have a property in masterpage which i populate in page render event as below:

我在masterpage中有一个属性,我在页面呈现事件中填充如下:

public string myproperty{get; set;}
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    myproperty = "TEST";          
}

on my aspx page I have a <%@ Reference VirtualPath="~/Site.Master" %> and on page init event I access it as so:

在我的aspx页面上,我有一个<%@ Reference VirtualPath="~/Site。Master" %>,在页面init事件中,我可以访问它:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Site msPage = (Site)Page.Master;
    string t = msPage.myproperty ;
} 

I can see the property on masterpage having a value but on aspx page it is always null. am I doing something wrong can someone help please.

我可以看到主页上的属性有一个值,但是在aspx页面上,它总是null。我做错什么了吗有人能帮忙吗?

2 个解决方案

#1


3  

I can recommend you to read this:

我建议你读以下内容:

http://dotnetbyexample.blogspot.be/2007/10/right-way-of-accessing-master-page.html

http://dotnetbyexample.blogspot.be/2007/10/right-way-of-accessing-master-page.html

#2


0  

The property isn't initialised yet.

该房产尚未被初始化。

The master page is actually implemented as a control in the page, so the page is initialised before the master page.

主页面实际上是作为页面中的控件实现的,因此页面在主页面之前被初始化。

#1


3  

I can recommend you to read this:

我建议你读以下内容:

http://dotnetbyexample.blogspot.be/2007/10/right-way-of-accessing-master-page.html

http://dotnetbyexample.blogspot.be/2007/10/right-way-of-accessing-master-page.html

#2


0  

The property isn't initialised yet.

该房产尚未被初始化。

The master page is actually implemented as a control in the page, so the page is initialised before the master page.

主页面实际上是作为页面中的控件实现的,因此页面在主页面之前被初始化。