I have a master page with one property name event type. Now I want to set this property from a content page and then that property value should be available to another content page.
我有一个包含一个属性名称事件类型的母版页。现在我想从内容页面设置此属性,然后该属性值应该可用于另一个内容页面。
Is this possible with asp.net? If yes then please help me out.
这可能与asp.net?如果是,请帮助我。
And yes, my content page already inherits another page which is not master.
是的,我的内容页面已经继承了另一个非主页面。
7 个解决方案
#1
1
If this property is a custom property that you added to your master page, then you'll have to add a MasterType
declaration to the page that is incorporating it.
如果此属性是您添加到母版页的自定义属性,则必须将MasterType声明添加到合并它的页面。
<%@ MasterType
virtualpath="~/Path/To/Your.master"
%>
This allows the web site or application to know the specific type of the master page at compile time and allows you to access it as you would any other property in a control.
这允许网站或应用程序在编译时知道母版页的特定类型,并允许您像控件中的任何其他属性一样访问它。
Page.Master.MyCustomerProperty = someValue;
Edit: As a side note, in getting this property down to your next control, it would probably be best to create (and raise) a custom event indicating the property has changed. This way a number of controls can subscribe to the event and "self-update" without having to be concerned with the timing of when the property is set.
编辑:作为旁注,在将此属性下载到下一个控件时,最好创建(并引发)指示属性已更改的自定义事件。这样,许多控件可以订阅事件并“自我更新”,而不必关心设置属性的时间。
Example: In your master page you can define an event as a "global" variable. Then in your property you can raise this event.
示例:在主页面中,您可以将事件定义为“全局”变量。然后在您的财产中,您可以举起此活动。
public event EventHandler myPropertyChanged;
public delegate void MyPropertyChanged(object sender, EventArgs e);
//...
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
if (myPropertyChanged != null)
myPropertyChanged(this, new EventArgs());
}
}
Then in your other controls, you can subscribe to this event to know when it changes:
然后在您的其他控件中,您可以订阅此事件以了解它何时更改:
protected void Page_Load(object sender, EventArgs e)
{
Page.Master.MyPropertyChanged += new EventHandler(MasterPropertyChanged);
}
protected void MasterPropertyChanged(object sender, EventArgs e)
//Rememeber you need the VirtualType in order for this event to be recognized
SomeLocalValue = Page.Master.MyProperty;
}
A good step-by-step tutorial of this process can be found on CodeProject. A good c# custom events tutorial can be found on the MSDN.
可以在CodeProject上找到此过程的详细分步教程。可以在MSDN上找到一个好的c#自定义事件教程。
#2
2
If your pages are from different requests, a master property will not work. The master page is not static, there is a different one for each request, which means that the instance you set the value on is gone when you try to retrieve it. The Session
dictionary is where you should put user content that you want to persist between requests.
如果您的页面来自不同的请求,则主属性将不起作用。母版页不是静态的,每个请求都有一个不同的页面,这意味着当您尝试检索它时,您设置值的实例已经消失。在会话字典中,您应该放置要在请求之间保留的用户内容。
#3
2
Add a property to the master page:
将属性添加到母版页:
public string lblBannerText { get { return lblBanner.Text; } set { lblBanner.Text = value; } }
Then, get the master of the page and cast it to the master class type:
然后,获取页面的主人并将其转换为主类类型:
((MyMaster)Page.Master).lblBannerText = "banner text";
#4
0
You can use Master property
of your page and access to your property
您可以使用页面的主属性并访问您的财产
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.page.master.aspx
链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.page.master.aspx
Sample
样品
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
mpTextBox =
(TextBox) mpContentPlaceHolder.FindControl("TextBox1");
if(mpTextBox != null)
{
mpTextBox.Text = "TextBox found!";
}
}
#5
0
You can try this
你可以试试这个
Create a property in your master page and you access it from content page:
在母版页中创建一个属性,然后从内容页面访问它:
Master page:
母版页:
public partial class BasePage : System.Web.UI.MasterPage
{
private string[] _RequiredRoles = null;
public string[] RequiredRoles
{
get { return _RequiredRoles; }
set { _RequiredRoles = value; }
}
}
Content Page:
内容页:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load()
{
Master.RequiredRoles = new string[] { //set appropriate roles };
}
}
#6
0
the easy way to get or set MasterPage's property, just like this.in content page do the follow.
获取或设置MasterPage属性的简便方法,就像这样。在内容页面中执行以下操作。
protected override void Render(HtmlTextWriter writer)
{
var master = this.Master as Site;
master.SiteName += "|网站首页";
base.Render(writer);
}
and define property in master page
并在母版页中定义属性
public partial class Site : System.Web.UI.MasterPage
{
public string MetaDescription { get; set; }
public string MetaKeywords { get; set; }
public string SiteName { get; set; }
public SiteSettings siteSettings { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
siteSettings = SettingsFactory<SiteSettings>.Get();
MetaDescription = siteSettings.SearchMetaDescription;
MetaKeywords = siteSettings.SearchMetaKeywords;
SiteName = siteSettings.SiteName;
}
}
#7
0
I had a similar problem. Here's what I did:
我有类似的问题。这是我做的:
//Id the form tag and place a runat server
<form id="form1" runat="server" >
//In C# Masterpage.cs - Expose the forms properties
public System.Web.UI.HtmlControls.HtmlForm Form1 {
get { return form1; }
set { form1 = value;
} }
//In C# Consuming page add this to pageLoad using UniqueId
((SiteMaster)Page.Master).Form1.DefaultButton = btnSearchUsersLink.UniqueID;
#1
1
If this property is a custom property that you added to your master page, then you'll have to add a MasterType
declaration to the page that is incorporating it.
如果此属性是您添加到母版页的自定义属性,则必须将MasterType声明添加到合并它的页面。
<%@ MasterType
virtualpath="~/Path/To/Your.master"
%>
This allows the web site or application to know the specific type of the master page at compile time and allows you to access it as you would any other property in a control.
这允许网站或应用程序在编译时知道母版页的特定类型,并允许您像控件中的任何其他属性一样访问它。
Page.Master.MyCustomerProperty = someValue;
Edit: As a side note, in getting this property down to your next control, it would probably be best to create (and raise) a custom event indicating the property has changed. This way a number of controls can subscribe to the event and "self-update" without having to be concerned with the timing of when the property is set.
编辑:作为旁注,在将此属性下载到下一个控件时,最好创建(并引发)指示属性已更改的自定义事件。这样,许多控件可以订阅事件并“自我更新”,而不必关心设置属性的时间。
Example: In your master page you can define an event as a "global" variable. Then in your property you can raise this event.
示例:在主页面中,您可以将事件定义为“全局”变量。然后在您的财产中,您可以举起此活动。
public event EventHandler myPropertyChanged;
public delegate void MyPropertyChanged(object sender, EventArgs e);
//...
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
if (myPropertyChanged != null)
myPropertyChanged(this, new EventArgs());
}
}
Then in your other controls, you can subscribe to this event to know when it changes:
然后在您的其他控件中,您可以订阅此事件以了解它何时更改:
protected void Page_Load(object sender, EventArgs e)
{
Page.Master.MyPropertyChanged += new EventHandler(MasterPropertyChanged);
}
protected void MasterPropertyChanged(object sender, EventArgs e)
//Rememeber you need the VirtualType in order for this event to be recognized
SomeLocalValue = Page.Master.MyProperty;
}
A good step-by-step tutorial of this process can be found on CodeProject. A good c# custom events tutorial can be found on the MSDN.
可以在CodeProject上找到此过程的详细分步教程。可以在MSDN上找到一个好的c#自定义事件教程。
#2
2
If your pages are from different requests, a master property will not work. The master page is not static, there is a different one for each request, which means that the instance you set the value on is gone when you try to retrieve it. The Session
dictionary is where you should put user content that you want to persist between requests.
如果您的页面来自不同的请求,则主属性将不起作用。母版页不是静态的,每个请求都有一个不同的页面,这意味着当您尝试检索它时,您设置值的实例已经消失。在会话字典中,您应该放置要在请求之间保留的用户内容。
#3
2
Add a property to the master page:
将属性添加到母版页:
public string lblBannerText { get { return lblBanner.Text; } set { lblBanner.Text = value; } }
Then, get the master of the page and cast it to the master class type:
然后,获取页面的主人并将其转换为主类类型:
((MyMaster)Page.Master).lblBannerText = "banner text";
#4
0
You can use Master property
of your page and access to your property
您可以使用页面的主属性并访问您的财产
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.page.master.aspx
链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.page.master.aspx
Sample
样品
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
mpTextBox =
(TextBox) mpContentPlaceHolder.FindControl("TextBox1");
if(mpTextBox != null)
{
mpTextBox.Text = "TextBox found!";
}
}
#5
0
You can try this
你可以试试这个
Create a property in your master page and you access it from content page:
在母版页中创建一个属性,然后从内容页面访问它:
Master page:
母版页:
public partial class BasePage : System.Web.UI.MasterPage
{
private string[] _RequiredRoles = null;
public string[] RequiredRoles
{
get { return _RequiredRoles; }
set { _RequiredRoles = value; }
}
}
Content Page:
内容页:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load()
{
Master.RequiredRoles = new string[] { //set appropriate roles };
}
}
#6
0
the easy way to get or set MasterPage's property, just like this.in content page do the follow.
获取或设置MasterPage属性的简便方法,就像这样。在内容页面中执行以下操作。
protected override void Render(HtmlTextWriter writer)
{
var master = this.Master as Site;
master.SiteName += "|网站首页";
base.Render(writer);
}
and define property in master page
并在母版页中定义属性
public partial class Site : System.Web.UI.MasterPage
{
public string MetaDescription { get; set; }
public string MetaKeywords { get; set; }
public string SiteName { get; set; }
public SiteSettings siteSettings { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
siteSettings = SettingsFactory<SiteSettings>.Get();
MetaDescription = siteSettings.SearchMetaDescription;
MetaKeywords = siteSettings.SearchMetaKeywords;
SiteName = siteSettings.SiteName;
}
}
#7
0
I had a similar problem. Here's what I did:
我有类似的问题。这是我做的:
//Id the form tag and place a runat server
<form id="form1" runat="server" >
//In C# Masterpage.cs - Expose the forms properties
public System.Web.UI.HtmlControls.HtmlForm Form1 {
get { return form1; }
set { form1 = value;
} }
//In C# Consuming page add this to pageLoad using UniqueId
((SiteMaster)Page.Master).Form1.DefaultButton = btnSearchUsersLink.UniqueID;