event of asp.net master page not firing - asp.net

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

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.

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.

How does one get a code-behind file to recognize type of a user control?

I have a custom control in my mater page that represents a menu, let's call it CustomMenu. The control code files are located in a non special directory.
I want to give each page that uses that master page the ability to access the control and, using a public property, tell the control whether or not it should render itself.
I have a public property on the control to do that, and I know I can get the control by referencing Page.Master.FindControl('IdOfControlIwant');
The problem I'm having is that I can't seem to get the control Type recognized by the compiler, so when I do find the menu control, I can't actually work with it. The only way I can get the code behind to recognize the type is to register it in the ascx file, and then add at least one control to the page, which is undesirable.
Thoughts?
You have to combine what both Jacob and dzendras have posted. Add the MasterType directive to your content page's aspx file:
<%# MasterType VirtualPath="~/your.master" %>
And in the master page create a property:
public CustomMenu MyCustomMenu {get{ return myCustomMenu;}}
Where myCustomeMenu is the ID of the Usercontrol in your masterpage.
You should now be able to reference the usercontrol from a content page. So, if the CustomMenu usercontrol had a property called SelectedItem, you should be able to access it like this:
public void Page_Load(object o, EventArgs e)
{
Master.MyCustomMenu.SelectedItem = 1;
}
Use the MasterType directive in your pages:
<%# MasterType VirtualPath="~/your.master" %>
That will strongly type your Master page reference, so you should be able to add properties that can be accessed by the pages.
Make a property of your MasterPage class:
bool IsCustomMenuVisible {set{ CustomMenu.Visible = value;}}
And use it wherever you like.

Access body element from content page via a nested master page

All I want to do is access the <body> element from the code-behind of a content page and add a class name to it.
I have a top-level master page with the <body> element in it. Then I have a nested master page which is the master page for the content page. From the code behind of the content page I want to add a class name to the body element. That's all.
I have this in the top-level master:
<body id="bodyNode" runat="server">
I added this to the code-behind for the content page:
Master.bodyNode.Attributes.add("class", "home-page");
And I get a message that:
System.Web.UI.MasterPage' does not contain a definition for 'bodyNode
If I add this to the aspx content page:
<% # MasterType VirtualPath="~/MasterPage.master"%>
The message then changes to:
bodyNode is inaccessible due to its protection level
Please advise, I've wasted like 2 hours on what feels like something that should be really simple to do :(
once you have set runat="server" for your body node, you have to access it using the HTMLControls namespace. try this.
public void Page_Load(Object sender, EventArgs e)
{
//Inject onload and unload
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("bodyNode");
body.Attributes.Add("class", "home-page");
}
EDIT
Your problem is that you have nested master pages.
Since the "body" tag is in your top level master page, Master.FindControl() won't work, as that is looking in the nested master page.
What you need to do is use Master.Master.FindControl(), or recursively loop through your master pages, going up until Master.Master is null (as then you know you are at the top level master page) and then calling FindControl() on that.
I would add a public property to the code behind of the master page that would allow access to the body tag that is part of the master page. And then call that property from the content page.

How to change the Master Page dynamically

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...

Resources