Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Inheritance And Events

Back to Forum: Tech Off
  • User profile image
    themaffeo

    Ok, here's the situation:

    CustomPageClass inherits from System.WEb.UI.Page
    CustomPage2Class inherits from CustomPageClass
    MyAspxPage inheris from CustomPage2Class

    Ok, Each of the three classes has a method "PreRender" that handles the PreRender event of the base page class.

    .Net Event handeling says that each method WILL handle the event, my question was "in what order"

    Simple trial and error showed that the events cascaded down the inheritance tree: CustomPageClass handeled the event first, then CustomPage2Class, and finally MyAspxPage.

    My question is why?  I searched and searched but couldn't find the msdn reference to explain this behavior.  I know I have it "working"  but i'm one of those people that wants to see the documentation as well.  Anyone have any ideas where I can fidn that?

    thnx

  • User profile image
    bitmask

    The event handlers are executed in the order they were added. If you post the code you use to wire up the event I could give you a better explanation, but it sounds like you are wiring up the event in the constructors of each class.

  • User profile image
    themaffeo

    Hmm, ok, that could explaing the behavior.  Here is an outline of what the code looks like:

    Class Foo1
       inherits system.web.ui.page

       protected obj as MyCustomObject


         Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

    PersistObject(obj)

    End Sub

    end class

    Class MyPage
       inherits Foo1

     Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    obj=new MyCustomObject

    obj.name="fred"

    end sub 

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
       obj.name="bob"

    End Sub

    end class
    ----------

    So lets say that "persistObject" saves the object to some data store.  The name property that gets saved ends up being "fred" not "bob" since the obj is persisted before the final page's prerender event gets handled.

    What you are saying about the events makes sense though.  When the Mypage class gets instantiated, I believe the .net runtime recurses back up the tree and creates the objects at the top of the higherarchey down to the item.  In which case the handle event of the base class would get bound before the page class... sound about right?

  • User profile image
    footballism

    How about save your custom object's state in ViewState using SaveViewState method?

  • User profile image
    themaffeo

    footballism wrote:
    How about save your custom object's state in ViewState using SaveViewState method?


    Thanks, but same problem I think.

    Assuming I want to only want the base class to know about saving (freeing the developer of the derived class from having to think about it), and the developer makes a change to the object in the derived classes prerender event, My saving event has already fired, and the childs changes are not saved.

    Of course the solution is to 1) either move the offending code out of the child's prerender or 2) allow the chld to call the save metod as well if it so likes.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.