When is the ViewState collection available in the page life cycle?

When is the view state available from the .Viewstate property of the control? According to my observations, the closest event relates to the Page.PreLoad event (on the .InitComplete page, ViewState is still unavailable).

However, for controls that implement the IPostBackEventHandler interface, the LoadValue () method is called and the .Viewstate collection is available (this happens after Page.InitComplete and before Page.PreLoad).

Does anyone know of any additional events that can be used to get ViewState availability information? Or any tricks (not excluding reflection in private / protected / internal members) that can be used to determine if Viewstate has loaded or not?

+3
source share
1 answer

When is the view state available from the .Viewstate property of the control?

After executing the method LoadViewState.

This usually means after the Init phase and before the Load and Handlers phase (e.g. OnClick). But ViewState is really complex, so I highly recommend reading this great article to really understand ViewState.

Since you can override the method LoadViewState, this makes a good place to post any tricks you mentioned:

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        this.ViewStateLoaded = true; // or you could fire an event or something
        UpdatePanelVisibility();
    }

Of course, this assumes that you are using your own control implementation, which is not always the case.

+8
source

Source: https://habr.com/ru/post/1790756/


All Articles