How to change the Master Page dynamically - asp.net

I want to assign one master page dynamically for a pure aspx file, Anybody can tell me, how to do this?

You can override OnPreInit in your default.aspx.cs and set the master page based on some value in your querystring. Something like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.QueryString["Master"] == "Simple")
MasterPageFile = "~/Masterpages/Simple.Master";
}
EDIT: the cause of your error message might be covered by this question.

I Left the ContentPlaceholder to add on it.. Actually , I tried to assign master page without using ContentPlaceHolder.. Now, I realised that, atleast one ContentPlaceholder should be there temporarily, even though we will change the master page dynamically...

Related

aspx page showing old master page even after changing in page directive

This is either a weird behavior or I am doing something wrong here.
I have an aspx page with an associated Master page. I want to replace this master page with a new one.
The following steps I followed:
I created a new master page and added the same html from the old one.
I replaced MasterPageFile attribute in the page directive.
I thought this should work but it is not. It is still showing the old master page.
Now, when I replace the MasterPageFile from code it works.
public DefaultNew()
{
this.PreInit += new EventHandler(DefaultNew_PreInit);
}
void DefaultNew_PreInit(object sender, EventArgs e)
{
MasterPageFile = "~/_Master/MasterPageNew.Master";
}
I have rebuilt the code, closed Visual Studio and restarted but in Vain.
An ideas? Please help.
Check to see if you have a base class for the page that happens to sets the master page for all pages that derive from it.

Aspx file in two master pages

I have a page on a website that uses a master page and a child page. I would like to use the child page to create a new page on the website, but with a different master page.
Is it possible to do this without duplicating the code for the child page?
To make it clearer I have also added a link to a picture of the layouts used for the two pages.
In the picture, both children use the same code.
Edit: I have also thought about using a control for this, but I am not sure if this is the proper solution. The child page is pretty big and complex and also uses a lot of JavaScript.
When loading the child page, you can set the master page dynamically in code, in the PreInit event. Something like this:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
That way your child page can set its master page based on whatever condition you would have in your site.

updating master page image from content page

Have made several attempts to update an image in a master page div when switching between content pages. I first tried creating a master page property that I could update from the content page's Page Load event by declaring "MasterType VirtualPath=", but since the master is already loaded by this time it wasn't going to work. It did work when I set the ImageUrl in the master page's page load event (if !Page.IsPostBack then set the image's url attrib), so I know it can work, but I need the image to change for every content page I visit.
Then I tried using the master's menu button click events to set the ImageUrl before loading the content page but this also had no effect. I saw a thread suggesting the use of an UpdatePanel to hold the image, so I may try that next. What's the best way to do this..??
I wouldn't be surprised if a better way is to have the image in a content div instead, and updating that from the master. Any suggestions or links would be most welcome. I can post code if anyone would like to have a look. thanks.
I don't know why you found it difficult. There are many ways to do this but I'll only show one. I just tested this and it worked. How?
In your master page, define your image and add runat="server"
<img src="put your default image.jpg" runat="server" id="changingImage" />
In your content pages, do this
protected void Page_Load(object sender, EventArgs e)
{
HtmlImage img = Master.FindControl("changingImage") as HtmlImage;
img.Src = "~/images/imageForContentPage1.jpg"; //replace this image based on your criteria
}
Possible exception is Null Reference when the name of the image control specified in .FindControl could not be found. Make sure it's exactly as you named it in the Master Page. And to prevent a Null Reference Exception, wrap a check around
if(img != null)
{
img.Src = "~/images/imageForContentPage1.jpg";
}

Checking Request.ServerVariables["HTTP_REFERER"] on every request

I am using this code to check if the request came from a page , if not then redirect somewhere.
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
It is working , I don't know whether it is perfect the solution.However I am checking this on load event of one of my page.How can I make it check on every request.Should I check this for all my pages.Also it is a good approach.Can anybody point me in the right direction.Any suggestion is welcome.
If you have logic that you would like to be run on the OnLoad of a bunch of your pages. You should probably create a BasePage that derives from Page and have the logic inside. Then all the pages you want that logic in can derive from BasePage instead of the regular Page.
Another approach can be using Master Pages
Note: After reading OPs additional comments. One thing to look out for when using a Master Page is that the Master Page's Page_Load event happens AFTER the Content Page's Page_Load event.
In other words the lifecycle is like this:
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
If your response.redirect moves the user to another page with the same master page (and same "validation" check) you might find yourself in an endless loop :)
If you have lot of pages, with these kind of common codes, than one possible solution is creating your own MyPage class as a child of the standard Page class. In your MyPage you can use something like:
Page_Load(object sender, EventArgs e)
{
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
base.Page_Load(sender, e);
}
Then any of your pages can inherit from this own MyPage class instead of the .NET's standard one.
In this way the common code reside in one place. In case of any change you have to modify that only there.
Or another possibility, you can consider using Master Pages.

event of asp.net master page not firing

I put a break point at the protected void Page_Load(object sender, EventArgs e) method of my master page, but when i start the site it does not hit that break point.
Why is the event not firing? I would like to use this event along with others such as the Init event in order to check to see if the session has expired everytime a page loads...
Thanks.
You can check that AutoEventWireup is set to true in the Master declaration.
<%# Master Language="C#" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="MasterPages_Main" %>
If it is set to false you have to manually connect the events.
The problem is likely that your .aspx page has not correctly referencing your .master page. Be sure that, at the top of your .aspx page, you have a line similar to the following:
<%# Page Title="Some Title" Language="C#" MasterPageFile="Main.Master" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" %>
Another possible problem is that your .master page isn't referencing the proper (or any) assembly. Be sure that the top line of your .master page is similar to the following:
<%# Master Language="C#" AutoEventWireup="True" CodeBehind="Main.master.cs" Inherits="MyApp.Main" %>
A couple of things to check, some of which may be obvious...
Check your child page is calling the correct Master Page.
The Master Page Page_Load executes after the child Page_Load, so make sure you debug through the child page execution first.
Check that you've actually got your Page_Load event wired up if you're using VB.NET.
You might want to try creating a base class of type Page that handles your session check. Leave the master pages for page design. If you have multiple master pages, you would have to duplicate that code in each one, but if your pages inherit from a single base page, your session check logic will be in one place.
I had the same problem - the pageload was firing before but something went wrong and it stopped.
What fixed it was putting the page_load event in the .master file not the .master.cs
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//put your code here
//any function u wanna call declare it in the code file as public
}
</script>
I had a slightly different problem and different solution.
Just in case anyone has similar situation as me.
I had a nested master page and the control and related event method were in the "middle" master. The methods did NOT get called when they were placed in .cs file for middle master page. But they got called when included in .master page within script tags as described above by "petra".
This seems to be more of a bug in .net platform - Also - I do not think some of the above complicated solutions are (or should be) needed (e.g., keeping code out of master page and using master page only for structure etc.) - that is more of a workaround and I suspect there indeed is a bug in .net platform with respect to master page event firings (specially with nested master pages as in my case).
You need to check declaration of page to make sure it refers proper masterpage and masterpage, to make sure that it refers proper inherited class.
My error occured because of comment line >>> base.OnLoad(e); in Site.Master.cs
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
PEACE

Resources