Setting a master page's master page programmatically - asp.net

Is there anyway to set a Master Page's Master Page programmaticly? Would I need to do this on each page? I don't have access to the pre_init event from Master. Hrm...

In the PreInit function place:
this.MasterPageFile = "~/masterPage.master";
Yes, you will need to put this on each page. To get aroud it, have all the pages inherit from a base Page and place put the master page reference there.

void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
Working with ASP.NET Master Pages Programmatically

Related

No LoadComplete for a MasterPage?

With AutoEventWireUp set to false, I would normally set a Page_Finalize like this:
Include(Self.LoadComplete, Self.Page_Finalize); //Delphi
LoadComplete however, doesn't seem to be available on a MasterPage and my Page_Finalize obviously doesn't get called.
What am I meant to do to free objects in the master page?
Thanks.
LoadComplete method is simply not a member of MasterPage, but of Page.
There are several reasons for this, including the fact that the orchestrator of page life cycle is Page class itself.
It has three event-related methods: PreLoad, Load, LoadComplete. During Load, the Load event of children controls is triggered.
While a master page contains (by means of layout) the contents of the page, the page contains the MasterPage by means of objects, since the Page is the IHttpHandler that responds to HTTP requests in ASP.NET.
Briefly, you cannot override (I don't know Delphi, is that some kind of override?) OnLoadComplete in MasterPage as it's not defined. Only OnLoad
A bit late, but adding a method to the Page.LoadComplete event from the master page seems to work.
Within the master page:
protected void Page_Load(object sender, EventArgs e) {
Page.LoadComplete += Page_LoadComplete;
}
protected void Page_LoadComplete(object sender, EventArgs e) {
// do stuff on LoadComplete in the master page
}

How to access content page controls from master page in asp.net

it is very easy to access master page control from content page like
protected void Page_Load(object sender, EventArgs e)
{
// content page load event
DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
userLabel.Text = thisDropDown.SelectedValue;
}
but how could i access controls of content page from master page. suppose a textbox there in content page and one button is there in master page. i want that when i will click on master page button then i want to show the text of textbox in the content page in the label of master page. how to achieve it. please help me with code sample. thanks.
In master page button click event should access page contents by:-
protected void Button1_Click(object sender, EventArgs e)
{
TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");
if (TextBox1 != null)
{
Label1.Text = TextBox1.Text;
}
}
It's been a while, but I believe you can do so by using the ContentPlaceHolder as a reference:
Control control = this.myContentPlaceHolder.FindControl("ContentPageControlID");
In my opinion it even better to use event raise from Master page and catch this event in contenet page for changing some contenet on this page, for instance. The main advantage is reusability. In future you may want to change content on other content page from the Master page and in this case you should only add event handler to this content page without changing code on master page. Within such approach you needn't hardcode control name from some content page. And moreover you shouldn't add dependency for some content's control at all.
A sample of implementation you can find here, for example.
you should look for contentplaceholder from master page then contentplaceholder in child of the master page
this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
You can find control by using this:
ContentPlaceHolder contentPage = Page.MasterPage.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label lblHead =(Label)contentPage.FindControl("lblHeading");
Response.Write(lblHead.Text);
Source:
http://xpode.com/ShowArticle.aspx?ArticleId=629
C# code within Site.Master:
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
Code within Site.Master.cs:
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
ToolkitScriptManager1.RegisterPostBackControl(this.MainContent.FindControl("btnOnDefaultPage"));
}
}
This is an example how a client control on the Default.aspx is referenced from a Site.Master.cs
Try this code
Page.Master.FindControl("MainContent").FindControl("DivContainer_MyProfile").Visible = True

Asp.net master page

how to Bind webpages that's already created with a new master page ?
AT Page_PreInit event you can set master page.
for example
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "Master.master";
}
You will need to change the markup in the .aspx file something along the following lines
1) Remove html head & body
2) Insert ASP:CONTENT tag
3) Insert MasterPageFile attribute in #PAGE directive
To be honest the easiest way might be to just create a new content page, select the masterpage and then copy any markup you need into the panel and copy your codebehind over too.

How to dynamically change master page's master page?

I am trying to change the master page dynamically, and although it's easy to do from a content page (overriding OnPreInit), there is no such event for a master page. Is it possible to introduce this event somehow?
UPDATE: I got halfway there by going via the PreInit of the pages at the bottom of the ladder, turns out you can do things like base.Master.MasterPageFile = "/master.Master";, but for some reason this doesn't load the stuff in the header of the top-most master page, namely stylesheets.
Quoting from: Can I change a nested master page's master dynamically?
Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
If you overrode the MasterPageClass and added your own onPreInit you might could do it, but I don't think even that would work. There's definitely no construct for it according to Reflector, nothing to even override, altho since it inherits UserControl then there's always OnInit ... alternately you could attempt to override get_Master() but that might not work either ...
Use the masterpage constructor.
Let's say you want to use a different master page without a menu, pass query string NoMenu.
protected void Page_PreInit(object sender, EventArgs e)
{
//You'll go through infinite loop if you do not check if we already have the new master page, this will switch to different master page if requested without a menu for example
if (Request.QueryString["NoMenu"] != null && this.MasterPageFile != "/MasterPageNoMenu.master")
{
this.MasterPageFile = "/MasterPageNoMenu.master";
base.OnPreInit(e);
}
}

Can I change a nested master page's master dynamically?

Okay, so we all know about changing a master page dynamically in a page's OnPreInit event.
But what about a nested master page? Can I change a master's master?
There is no OnPreInit event exposed in the MasterPage class.
Any ideas?
Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
We combine Andy's method with a "BasePage" class - we create a class that inherits from System.Web.UI.Page, and then all our pages inherit from this class.
Then, in our base page class, we can perform the relevant checks to see which root master page should be used - in our case we have a "Presentation" master, and an "Authoring" master - the presentation version has all the navigation and page furniture, along with heavy display CSS, while the authoring master has some extra JS for the authoring tools, lighter CSS, and no navigation (it's what we use when the user is actually authoring a page, rather than modifying the site layout).
This base page can then call Page.Master.MasterPageFile and set it to the Authoring master if that is the correct state for the page.
Just in case anyone stumbles across this and tears their hair out with a "Content controls have to be top-level controls in a content page or a nested master page that references a master page" error when trying Andy's code, get rid of the this.Master. So, the code becomes:
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "/Site2.Master";
}
Edit As Zhaph points out below, the code I have ^^ there will only change the current page's master, not the master's master. This is the code Hainesy was talking about when he mentioned "we all know about changing a master page dynamically" (which I didn't, d'oh). If you happen to get to this page by googling "stackoverflow change master page" (which is what I did) then this is possibly the code you're looking for :-)
To add on to the answer of Zhaph - Ben Duguid, (+1 by the way):
Here is example code that sets the master page of the nested master page. All pages inherit from this BasePage, so this code only exists in one place.
public class BasePage : System.Web.UI.Page
{
private void Page_PreInit(object sender, System.EventArgs e)
{
if (Request.Browser.IsMobileDevice)
{
if (Page.MasterPageFile == "~/master/nested.master"))
{
Page.Master.MasterPageFile = "~/master/mobile.master";
}
else
{
MasterPageFile = "~/master/mobile.master";
}
}
}
}

Resources