2
votes

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.

8
In case if you have multiple masterpages, Is your page hooked onto the right master-page with break-point ?this. __curious_geek
@this - one master page, deff hooked up correctly.kralco626
@Dave - well I did. the protected void Page_Load(object sender,EventArgs e) is my code. I have a break point at the point in my code. I run the site in visual studio and the page loads (with all the formatting from the masterpage) but the break point is never hit. I thought any additional code would just be a distraction from the main problem.kralco626

8 Answers

2
votes

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.

6
votes

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.

3
votes

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" %>
2
votes

A couple of things to check, some of which may be obvious...

  1. Check your child page is calling the correct Master Page.

  2. The Master Page Page_Load executes after the child Page_Load, so make sure you debug through the child page execution first.

  3. Check that you've actually got your Page_Load event wired up if you're using VB.NET.

1
votes

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>
1
votes

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

0
votes

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.

0
votes

My error occured because of comment line >>> base.OnLoad(e); in Site.Master.cs

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
}

PEACE