ASP.Net PreInit

Soldato
Joined
25 Mar 2004
Posts
15,922
Location
Fareham
Hi all,

I am trying to run a couple of public voids in my C# code before the aspx page initialises. I tried to insert into my Page_Load code but on the first load my content loaded before my voids could run. Subsequent refreshes were fine.

I found this code that worked, but it seems a bit filthy to me? should I really be using a protected override void to get access to the OnPreInit method of the page?

Code below:

Code:
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            // Insert Code Here
        }
 
Im no expert on WebForms, but isn't PreInit an event?

Code:
        public MyPage() : base()
        {
            PreInit += MyPreInit;
        }

        protected void MyPreInit(object sender, EventArgs e)
        {
            //stuff
        }

Though, I think yours should be ok too. It's virtual for a reason.
 
There's nothing wrong with overriding the PreInit method; the code-behind for an ASP.Net page is still a c# class after all :)

akakjs
 
I don't see anything wrong with that. There's nothing at all wrong with overriding base class behaviour with your own.

If you weren't meant to, or there would be other side-effects, the method would have been sealed.
 
Back
Top Bottom