Inherited Page Always Calls Base Page Load Event

  • Thread starter Thread starter WFB
  • Start date Start date
W

WFB

Hi,

I have a base class from which all of my pages derive (ABCBasePage). For
example, ABCCustomerSelect Inherits ABCPasePage. I would now like to have
ABCPocketSelect which should inherit from ABCCustomerSelect. My problem is
that when ABCPocketSelect is loaded the Page_Load event in ABCBasePage is
called, followed by the load event for ABCCustomerSelect - and I would like
to skip the ABCCustomerSelect load event..

My basic code is as follows:

Public Class ABCBasePage : Inherits Page
Private Sub Page_Load(Sender as Object, e as System.EventArgs) Handles
MyBase.Load
'Do code here that should happen on ALL pages
End Sub
End Class

Public Class ABCCustomerSelect : Inherits ABCBasePage
Private Sub Page_Load(Sender as Object, e as System.EventArgs) Handles
MyBase.Load
'Do code here that should happen ABCCustomerSelect only
End Sub
End Class

Public Class ABCPocketSelect : Inherits ABCBasePage
Private Sub Page_Load(Sender as Object, e as System.EventArgs) Handles
MyBase.Load
'Do code here that should happen ABCPocketSelect only
End Sub
End Class

Thanks for any help.

Joe
 
You'll want to do a couple things...

First, remove the "Handles MyBase.Load" from your Page_Load method in your
classes (like ABCCustomerSelect ). The Handles statement automatically
registers that method for an event, behind the scenes. Also, make
Page_Load a public method. We'll refer to it later from your child classes.

Next, add a "Sub New" to these base classes, as this will be your class
constructor. In this method, you'll want to register your Page_Load event
by hand. That command is:

AddHandler Load, Page_Load


So, now everything should function like it currently does -- the next step
is to remove the event handler as needed. In your child class
(ABCPocketSelect) you should put the following lines in its Sub New....
AddHandler Load, Me.Page_Load ' register its own page_load
RemoveHandler Load, MyBase.Page_Load ' remove its parent's page_load


So, check out AddHandler and RemoveHandler in help, it will explain more
about what's going on there.


-Charlie
 
Back
Top