Avatar of kgjertsen
kgjertsen
 asked on

VB to C# - Handles MyBase.Load

Hi,

I am converting some VB code to C# for use in a website and I have a problem.

All of the pages have a base class, that includes a Page Load event.:

Private Sub BasePage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Some Code.

End Sub

Every website page derives from this page. The base page Load code fires first, then the derived page Load code.

I need to use the same approach in C#, but the Handles MyBase.Load is not supported in C#.

Can anyomne provide the C# equivelant?

Many thanks,
Karl
.NET ProgrammingVisual Basic.NETEditors IDEs

Avatar of undefined
Last Comment
kgjertsen

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

asp.net pages have the page_load event!

http://www.w3schools.com/aspnet/aspnet_events.asp
kgjertsen

ASKER
The base class is class, not an ASP.NET page.

The code in the VB works, as the Handles keyword ensures the event fires when the base page is loaded. I am not sure how to do this in C#
Bob Learned

The default for C# web pages is AutoEventWireup = True.

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

    protected void Page_Load(object sender, EventArgs e)
    {
    }


Bob
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bob Learned

If you call it from a class, then you need attach an event handler:

this.Load += Page_Load;

Bob
kgjertsen

ASKER
Bob,

Many thanks.

Does this have to go into the base class or into the website page? Where will I need to put it? Could you provide a brief example?

I have been using VB for years, but I have only recently moved over to C#.

Thanks,
Karl
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kgjertsen

ASKER
Bob,

Thank you, you are a star!

I had got close by calling the load event from the class constructor, but got some strange results.

Thanks again.

Regards,
Karl
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.