PreRender in ASP.NET Master Page not firing
Asked Answered
T

1

6

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?

Taynatayra answered 8/12, 2011 at 12:49 Comment(6)
why do you expect that the method Pool will be called at all inside the class myMaster?Blub
Sorry, this is the constructor. Copy+paste-fail while copying code from several windows into the post.Taynatayra
do you call a page which references to your master page?Arva
I do. This works great so far, including ASP.NET correctly wiring and invoking Page_Init.Taynatayra
@Mudu - You have to post markup and code-behind of "said" master page. Read this thread too - #1168689Gilt
AVD, I updated the question with more information. I think I could narrow down the significant markup to this single line. Hope it helps.Taynatayra
S
-1

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.

Seamus answered 8/12, 2011 at 12:59 Comment(4)
Thanks to you Elias. Unfortunately is isn't yet. I double-checked my code, and everything is like you suggested.Taynatayra
This is totally nonsense @Mudu, you should try first and should let me know, thanks for your time.Seamus
We must have misunderstood each other. I did indeed try what you wrote, but it is not working. Besides, I did not vote down your answer, in case you feel offended.Taynatayra
Oh, sorry for blaming you, however I'm sure the voter did it intentionally without testing or without reading.Seamus

© 2022 - 2024 — McMap. All rights reserved.