I am trying to override the Page_PreInit function inside my class _Default which inherits from Page. However, when I try to compile I get the following error:
‘_Default.Page_PreInit(object, System.EventArgs)’: no suitable method found to override
Here is my code:
public partial class _Default : Page
{
protected override void Page_PreInit(object sender, EventArgs e)
{
// Todo:
// The _Default class overrides the Page_PreInit method and sets the value
// of the MasterPageFile property to the current value in the
// selectedLayout session variable.
MasterPageFile = Master.Session["selectedLayout"];
}
...
}
The
Pageclass declares a public event namedPreInitand a protected virtual method namedOnPreInit(which just raises thePreInitevent). So you have two options.Option 1 (recommended): Override
OnPreInit:Call
base.OnPreInit(e)so that the page raises thePreInitevent as usual.Option 2: Create a method named
Page_PreInit. ASP.NET will automatically bind this method to thePreInitevent as long as you don’t setAutoEventWireuptoFalsein the@Pagedirective or in Web.config.If you choose this option, don’t call
base.OnPreInit, or else you will end up with an infinite recursion.