ASP.NET Page base class differences in VB and C#


When you use a base class for your pages in VB and C# there are some subtle differences, if your base class implements event handlers for one or more of the Page events, such as Page_Load. In VB, you can simply use the Handles keyword, like this:

Public Class BasePage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    End Sub
End Class

This way, the Page_Load event will be called in the base class, and the page class derived from it (if the Page_Load event handler is implemented ;-). However, in C# there is no Handles keyword, and adding the AutoEventWireUp to the markup file like this, does not work with a base class:

<%@ Page Language="C#" ... AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> 

This will only wire up the Page events implemented in the Default class. One way to fix this is to override the OnLoad event in the base class, like this:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    ...

}
Advertisements