开发者

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 t开发者_如何学运维o 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...

  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜