event of asp.net master page not firing

11,192

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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>
Share:
11,192
kralco626
Author by

kralco626

Complicated is easy; it's simple that is hard.

Updated on June 16, 2022

Comments

  • kralco626
    kralco626 almost 2 years

    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.

  • kralco626
    kralco626 almost 14 years
    Thanks for your answer. Two things. First. My page is deffinatly reference the master page correctly. second, what is the inherits="MyApp.Main" mean? edit: oh and the break point i set it on the master page itself.
  • kralco626
    kralco626 almost 14 years
    sorry. should have said i'm using c# so 3 doesn't apply. deff got 1 right. And the website loads up and everything. I use it etc. it should have called the master page Page_load method before then right?
  • MCain
    MCain almost 14 years
    Create a class "MyCompany.BasePage" of type Page. Add your session check in Load. Then, in your codebehind of each of your pages, change the type from Page to MyCompany.BasePage
  • kralco626
    kralco626 almost 14 years
    should it be .BasePage.cs? I went to add item and selected "class and called it MySite.BasePage
  • MCain
    MCain almost 14 years
    Sure, create a .cs class. Have it inherit System.Web.UI.Page. Implement Page.Page_Load in BasePage. Then inherit BasePage in your other pages.
  • kralco626
    kralco626 almost 14 years
    I used the following solution: dreamincode.net/forums/topic/… Copy the code at the botttom and have your page inherit SessionState rather than Page
  • crenshaw-dev
    crenshaw-dev over 5 years
    I read this and thought "no way." My case was almost the simplest possible use case. The page directly used the master file, and the master file was simply the direct child of a root master file. Moving the Page_Load event method to code front in <script> tags did the trick. Thanks!