views:

97

answers:

2

I am creating a custom control and I need to handle some logic AFTER LoadViewState (which may or may not be called depending on if the control was visible or not) and BEFORE LoadPostBack (which may or may not be called depending on if the control was visible or not).

Is there anything in the control lifecycle that will accommodate this?.

I need this because I want to do some processing if the view state WAS NOT loaded. I don't want to do it in init because I would do it there, then potentially have to do it again when the view state is loaded.

So I need to be able to run code when the view state is not loaded (a.k.a when LoadViewState is not called) AND AT THE SAME PLACE in the life-cycle of the control (after Init, before LoadPostData)

A: 

Init is fired before LoadViewState, Load is fired after LoadViewState. I don't know though that Load is fired before LoadPostBack... By default, LoadViewState is not called on the initial page load, but called all other times... so I'm assuming you have custom viewstate handling of this control and am doing something special? The other thing you can do, is after LoadViewState method runs, put a custom event and fire this event at the end of the LoadVIewState routine...

Brian
`LoadViewState` is not called if the control was not rendered to the browser (aka it was set to visible). If `LoadViewState` was called every postback then it wouldn't be a problem
Bob Fincheimer
Welllllllllll, are you sure about that? An invisible control can still have its properties changed (as it still exists within the control tree) and so that would mean any changes when invisible would not be saved and reloaded on next postback?
Brian
A: 

LoadViewState is not called on every postback, but LoadControlState is. In Init:

Page.RegisterRequiresControlState(Me)

and then i implemented:

Protected Overrides Sub LoadControlState(ByVal savedState As Object)
     ' Always called as long as I save something to controls state

     Dim p As Pair = savedState
     MyBase.LoadControlState(p.First)

     If Not CType(p.second, Boolean) Then
         '  I store whether or not the control was rendered last request
     End If

End Sub

Protected Overrides Function SaveControlState() As Object
    Return New Pair(MyBase.SaveControlState(), Visible)
End Function

Therefore LoadControlState is always called on every postback and I can handle my processing accordingly

Bob Fincheimer