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.
And yes, my content page already inherits another page which is not master.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
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.
Method 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.
Method 3
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";
Method 4
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
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!";
}
}
Method 5
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 };
}
}
Method 6
the easy way to get or set MasterPage’s property, just like this.in content page do the follow.
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;
}
}
Method 7
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;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0