multiple master pages for 1 calling page - asp.net

I have 2 master pages with same (href) links on top of the page. Now these links load on the same master page when clicked on (by requirement). The problem is both these master pages have same links, so basically the same page shall be loaded in the master page.
Now what i need is when the person is on Master Page 1 and clicks on the link it should load in the same page. Whereas if the user is on Master Page 2 and clicks on the same link, i should be able to change the master page from 1 to 2 and load that in Master Page 2. Something like DirectCast.
any ideas?

You can switch the master page in the Page_PreInit event. For more of an explanation check out http://msdn.microsoft.com/en-us/library/ms178472.aspx
It would look like
void _Default_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "NewMasterPage.master";
}

I don't think I fully understand your use case, but here are some ideas:
You could track which MasterPage the user is on through Session state, and use that to remember which MasterPage to show in subsequent visits.
If you can alter the hrefs slightly, you could use a query string to indicate which MasterPage should be utilized.
Depending on your application, you could store which MasterPage should be used in your database, tied to each user.
I assume you're dynamically switching MasterPages based on some logic, so it just comes down to choosing a method to store which MasterPage should be used.

Related

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.

Master pages in asp.net ? Execution of Master pages?

In asp.net, i have taken one master page, 4 four child pages and one login page, which is not in a master page. My question is when a user login redirects to the page which is has master pages, is master page execute every time or only one time after login.
Master-page-engine is just part of total page life-cycle. (see for full list of taken actions there: http://blogs.thesitedoctor.co.uk/tim/2006/06/30/Complete+Lifecycle+Of+An+ASPNet+Page+And+Controls.aspx)
So short answer - yes, each time page derived from master is shown - master is executed.
A master page is executed every time a requested childpage has it as it´s parent.
So if you go to your loginpage without master and then redirects to a page with the master, the masterpage is executed once. If you later request another page with that master or the same page again doesn´t matter. The masterpage will be executed again.
Study the ASP Page lifecycle as the masterpage has some strange behaivor and acts mote like a subcontrol. You have to be a bit careful with some events.

User control instances are null while using nested master page

I have couple of user controls which are statically referenced in aspx. We are setting some public properties on the user controls in Page_Preinit event.
The page also references a master page
This was working fine so far. Had to do a ui redesign and we implemented nested master pages.
Now, all of a sudden, the user controls are showing up as null. If I change the master page to parent one (instead of child -nested), it works fine.
Appreciate any pointers to this issue.
some sample code: here ucAddress is null
protected void Page_PreInit(object sender, EventArgs e) { ucAddress.City = "Dallas"; }
This blog post describes the problem very well and also offers a solution. I can confirm that the solution works and I'll repeat the relevant parts here:
The problem
We have a user control and initialize its control in the Init event so that it is ready when the ViewState is restored (between Init and Load).
Once you start using this encapsulation technique, it won’t be long until you want to pass in a parameter that affects the data you load. Before we do, we need to be aware that the Init event is fired in reverse order. That is, the child controls have their Init event fired before that event is fired at the parent. As such, the Page.Init event is too late for us to set any properties on the controls.
The natural solution is to try and use the Page.PreInit event, however when you do you’ll often find that your control references are all null. This happens when your page is implemented using a master page, and it relates to how master pages are implemented. The <asp:ContentPlaceHolder /> controls in a master page use the ITemplate interface to build their contents. This content (child controls) is not usually prepared until the Init event is called, which means the control references are not available. For us, this represents a problem.
The Solution
The fix is remarkably simple; all we need to do is touch the Master property on our Page and it will cause the controls to become available. If we are using nested master pages, we need to touch each master page in the chain.
The author of the blog post then offers a nice little snippet that you can execute in the PreInit handler of your Page that uses a MasterPage and contains a user control:
protected override void OnPreInit(EventArgs e)
{
// Walk up the master page chain and tickle the getter on each one
MasterPage master = this.Master;
while( master != null ) master = master.Master;
// Access now initialized user control
ucAddress.City = "Dallas";
}
Found the issue. had to initialize child master page before accessing user control.

Output cache in content and master page

Two questions:
1. If I have a content page and a master page and I put this inside my content page:
<%# OutputCache ...%>
Does it cache the whole page or only the content page portion?
2. How can I apply OutputChace in the master page?
I have a master page that has a lot of content pages that uses it. I want to apply the same outputcache profile on all of them, but I dont want to go one by one and change them.
Thanks.
The whole page is cached.
Edit
You can use user controls to cache portions.
As by the comments, if you want to cache all pages that are using a specific master page, you need the following code in the master page
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetValidUntilExpires(true);
}
Content page only will be cached; unless that content page is using the master page, in which case master page also will be cached.
Unlike a content page you can't use OutputCache directive for master page. See the below links
Cache Master Page in ASP.NET
http://www.dotnetperls.com/output-cache
http://forums.asp.net/t/1236981.aspx

Problem with Master pages event order

I have a simple setup with the master page housing some controls used by all child pages.
I found when moving to new pages the master page page loads event fires as a non post back and read the solution was to store it's current values somewhere for retrieval. Ok all done.
The child page uses these values to run a report. When I switch to a new report, all is well. If I change the values in the master page the master page and the sub page load events fire.
The load event for the sub page fires first, picks up the values from the master page which are still the old values and then finally the master page events fire and all the new values are stored. The report hasn't changed as it still ran from the old values.
I can't really see a way around this. All you ever hear is that master pages are a saving grace but I swear i've never jumped through so many hoops to get a page to load correctly.
And now this!
Anyone see a plan to resolve it?
Populating the controls during the Masterpage's Init will solve your issue from the sounds of it.
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
An alternate approach would be to have a public sub in the content page(s) that you can call from the masterpage during load which in effect acts as an alternate to the page load event.
A slightly more indepth look at the page lifecycle when using masterpages:
http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

Resources