开发者

When in the page lifecycle is the ViewState collection available?

When exactly is th开发者_如何学Ce 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜