PreRender in ASP.NET Master Page not firing

14.6k Views Asked by At

Both Page_PreRender and Page_Load do not work in the Master Page I am working with. Page_Init does, however, for any reason. AutoEventWireup is set to true.

public partial class MyMaster : MasterPage
{
    public MyMaster()
    {
        // tried this too, but doesn't matter whether this LoC
        // is there or not
        this.PreRender += Page_PreRender;
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        // does not fire
    }
}

I tried it out in an empty Web Project as well. There it works fine.

Edit: I figured out that setting EnableViewState to true fixes it:

<%@ Master Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true"
    CodeBehind="MyMaster.master.cs" Inherits="MyMaster" EnableViewState="false" %>

But I do not want the ViewState to be enabled. Overriding OnPreRender works as well, no matter what value EnableViewState has. Now I'm wondering why, and just using the override way seems a hacky to me. Can anybody help?

1

There are 1 best solutions below

4
On

I suggest to use AutoEventWireup in the page directive, so would you please try as below:

In your page directive <%@ Page ..., use AutoEventWireup="true" and in your master page, remove PreRender event subscription:

public MyMaster()
{
    // tried this too, but doesn't matter whether this LoC
    // is there or not
    //this.PreRender += Page_PreRender;
}

Hope everything is fine now, thanks for your time.

Edit: Please check in your web.config file and ensure that AutoEventWireup is not set to False.