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?

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79

1 Answers1

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

Elias Hossain
  • 4,410
  • 1
  • 19
  • 33
  • Thanks to you Elias. Unfortunately is isn't yet. I double-checked my code, and everything is like you suggested. – Matthias Meid Dec 08 '11 at 13:05
  • This is totally nonsense @Mudu, you should try first and should let me know, thanks for your time. – Elias Hossain Dec 08 '11 at 13:05
  • 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. – Matthias Meid Dec 08 '11 at 13:13
  • Oh, sorry for blaming you, however I'm sure the voter did it intentionally without testing or without reading. – Elias Hossain Dec 08 '11 at 13:19