开发者

How could I access the ViewState of the current page using HttpContext?

How could I access the ViewState of the current page using HttpContext

I have a ViewStateUtil class that I'd need to implement:

    public static T GetViewState<T>(ViewStateKey viewStateKey)
    {
       // how to imple开发者_如何学Pythonment it?! HttpContext.Current...?
    }


    private static T GetViewState<T>(string name)
    {
        return (T) ((BasePage)HttpContext.Current.CurrentHandler).PageViewState[name];
    }

I added a new PageViewState property and let all my pages inherit from my BasePage to expose ViewState then being able to get or set it.


It troubles me to inherit from a new Page class if you need only one quick and dirty acces to the current page's ViewState.

Reflexion is magic (if slow... do not make heavy using of this aceessor, of course!)

var pageType = typeof( Page );
var viewStatePropertyDescriptor = pageType.GetProperty( "ViewState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic );
var currentPageViewState = (StateBag)viewStatePropertyDescriptor.GetValue( HttpContext.Current.CurrentHandler );
// Now use currentPageViewState["whatYouWant"]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜