Friday, April 8, 2011

How can page events be handled from a usercontrol? (VB.net)

I would like some code to execute at the "preload" stage of my usercontrol's lifecycle. However the preload event is only available on the Page object. So, I added the following method to my usercontrol:

Private Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Page.PreLoad

However, I know get the compile error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types"

As the Page property is inherited from UserControl I don't see how this can easily be done.

Any help gratefully received!

From stackoverflow
  • Why not create a method that does whatever pre-loading needs to be done, and have your page call that method?

    Stephen Wrighton : The reason this is less than ideal, is that you don't want to have to add in that call to every page that you use the Usercontrol on.
  • Unless you go with Jason's option, then you're either stuck performing the action in the INIT event (which is before the Viewstate is loaded) or the PAGE LOAD event.

    My question is: Is there any reason that the action must be performed in the PreLoad as opposed to the Load events?

    Remember, the only thing that the Preload really does is load the Viewstate and PostBack data into the various controls (more info on the lifecycle).

    The LOAD event will occur prior to any Control Events (such as button clicks), so you will probably be just fine performing your action there. The only reason not too, is if you don't want the PostBack or Viewstate data to interfere with your process (for whatever possible reason) and then in that case, you can utilize the INIT event.

  • I apologize for giving a C# example but I can't remember how to do it in VB...

    Anyway, in your UserControl's Init you could explicitly assign your Page_PreLoad method as the handler for the PreLoad event of the UserControl's Page property instead of using the "Handles" syntax in the method declaration. What your example is doing is trying to assign an event handler to an event on the UserControl object for an event that the UserControl object doesn't raise. As you noted, UserControl doesn't inherit from Page, which is where the PreLoad event lives. UserControl does, however contain a Page object as one of its properties, which in turn exposes PreLoad as an event to which you can assign a handler. Anyway, this compiles and approaches what it sounds like you are looking for (using C-style comments to preserve WMD syntax highlighting).

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        // this assigns Page_PreLoad as the event handler 
        // for the PreLoad event of the Control's Page property
        AddHandler Me.Page.PreLoad, AddressOf Page_PreLoad
        MyBase.OnInit(e)
    End Sub
    
    Private Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs)
        // do something here
    End Sub
    

    I'm not sure if that serves your purpose--as Stephen Wrighton indicated above, there may be a better way with one of the different events in the page lifecycle. However, building on what he said, this should work because the control's OnInit is called in which the event handler assignment is made, then the Page's OnLoad event is raised and then the event handler within the control is executed.

    I Have the Hat : Thanks... VB has its pros and cons, but I never thought its syntax for assigning event handlers was intuitive. I initially posted in C# then went and looked up the VB version.
    Rabid : Think pre load is my new favourite part of the asp.net page lifecycle - that sweet spot after all request values have been processed and before load/control event handlers fire. Thanks.

0 comments:

Post a Comment