ASP.Net Web Forms - How to set properties of MasterPage from page and user controls?

You can create a base class form which your master pages inherit from that includes the properties you're looking to store.

Up vote 0 down vote favorite share g+ share fb share tw.

I have two master pages which are used by different content pages. I want to set master page properties from content page, so that master page can show some changes based on those values. And then I also need to access those master page properties in user controls added in master page to reflect some changes.

How to achieve that? I have found a way how to set master page properties from content page by adding and then using **Master. Property=value** but not sure about how to access user control.

Any ideas? C# asp.net usercontrols master-pages link|improve this question asked Aug 5 '11 at 0:56eFriend304111 68% accept rate.

You can create a base class form which your master pages inherit from that includes the properties you're looking to store: abstract public class MasterPageBase : System.Web.UI. MasterPage { public string Prop1 { get { return "Some Value"; } } } From your UserControl, you can then access the properties as follows: MasterPageBase masterPage = (MasterPageBase)this.Page. Master; string strTest = masterPage.

Prop1; // "Some Value.

I guess you can access user control defined in your master page in two ways: By using FindControl method, in order to use this you have to add the server tags for user control in content page too, in . Aspx page add this: then in code behind: myControl = (myControl)this.Page.Master. FindControl("userControl_Id"); or you can create a public property in your masterpage that returns your usercontrol: public myControl UserControl { get; set; } and in code behind of your content page you can access this user control simply through UserControl property: myControl ctrl = (myControl)this.Page.Master.UserControl.

I couldn't get the above answers to work, so here is what worked for me: You want to reference a master page property from a user control. Firstly, your master page will have a public property like so : public string BodyClass { set { this.masterBody.Attributes. Add("class", value); } } Now add a reference to the master page in the user control ASCX file like so : Then in the code behind (C# in my case) you have this code : Main masterPage = (Main)this.Page.

Master; masterPage. BodyClass = "container"; Without the reference to the master page above your user control will not be able to find the master page class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions