Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When in the page lifecycle is the ViewState collection available?

Tags:

asp.net

When exactly is the view state accessible from the .Viewstate property of a control? From my observations, the closest event is on the Page.PreLoad event (at Page.InitComplete, Viewstate is still unavailable).

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

Does anyone know of any additional events that can be used to know when Viewstate is available? Or any tricks (not excluding reflection on private/protected/internal members) that can be used to know if the Viewstate has loaded or not?

like image 477
WiseGuyEh Avatar asked Nov 18 '25 00:11

WiseGuyEh


1 Answers

When exactly is the view state accessible from the .Viewstate property of a control?

After the LoadViewState method has been run.

Normally this means after the Init phase and before the Load and Handlers (e.g. "OnClick") phase. But ViewState is really complicated, so I highly recommend reading this excellent article to truly understand ViewState.

Since you can override the LoadViewState method, this makes a good place to put any of the kind of tricks you mention:

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

Of course, this does assume that you are using your own implementations of controls, which is not always the case.

like image 170
StriplingWarrior Avatar answered Nov 21 '25 01:11

StriplingWarrior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!