Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Another C# / Vb.net question ...

Options
  • 27-03-2008 12:49pm
    #1
    Registered Users Posts: 7,468 ✭✭✭


    Okay so I've written a HttpModule in the past in C#. Basically what it does it grabs the request and attaches its own custom Load event to the page object. This event fires before the page's own Load event. If that's clear :pac:
    void IHttpModule.Init(HttpApplication context)
    {
    	context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }
    
    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
    	Page page = HttpContext.Current.CurrentHandler as Page;
    	if (page != null)
    	{
    		page.Load += new EventHandler(page_Load);
    	}
    }
    
    void page_Load(object sender, EventArgs e)
    {
    	// Does stuff
    }
    

    Hunky dorey. Works fine. However, now in Vb.net (or 2005) it doesn't work. The load event attached by the HttpModule fires after the page's own load event. :confused:
    Private Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.PreRequestHandlerExecute, AddressOf context_PreRequestHandlerExecute
    End Sub
    
    Private Sub context_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
        Dim page As Page = TryCast(HttpContext.Current.CurrentHandler, Page)
        If page IsNot Nothing Then
            AddHandler page.Load, AddressOf page_Load
        End If
    End Sub
    
    Private Sub page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Does stuff
    End Sub
    

    So it's no biggie as I've just used the PreLoad event instead which works fine for this particular problem. My question is though: Why doesn't the Vb.Net code behave the save as the C# code. Am I missing something? I'm assuming its to do with vb.net event handling. I'd really like to know as I'm too pushed for time to investigate fully but hate not understanding why something happens.


Comments

  • Registered Users Posts: 1,421 ✭✭✭Merrion


    I think that this is because the VB.NET built in event handlers (those that are defined using the Handles keyword rather than the AddHandler keyword) are wired up at the class creation point which would be before your AddHandler code is executed - so in this case PageLoad event has two attached handlers - the compilers one and your one.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    That sounds about right *sigh* I'm going back to c# on my next contract.


Advertisement