Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net ViewState - Even when disabled, some viewstate exist. Why?

Even when on the page, the EnableViewState property is disabled, I am still seeing some viewstate existing on the page:

"<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="VkBAB3n5LZYtY+nTzk1vEu1P/6QLf4qzFIKzpFRJe3DMf8UseUA/1RsO409HJX4QhkROSP0umoJvatjK/q+jXA==" />"

My question is why?

like image 343
TimLeung Avatar asked Jan 05 '09 15:01

TimLeung


1 Answers

It's the control state.

If you really want to get rid of viewstate and controlstate you can use this code in the code-behind for the page, or in any class that the code-behind derives from

class MyPage : Page {
    private class DummyPageStatePersister : PageStatePersister {
        public DummyPageStatePersister(Page p) : base(p) {}
        public override void Load() {}
        public override void Save() {}
    }
    private DummyPageStatePersister _PageStatePersister;
    protected override PageStatePersister PageStatePersister {
        get {
            if (_PageStatePersister == null)
                _PageStatePersister = new DummyPageStatePersister(this);
            return _PageStatePersister;
        }
    }

    // other stuff comes here
}

Be very careful when doing this, though, since you're violating the contract with the controls. MSDN explicitly states that control state is always available. In practice, however, it has worked for me.

Edit: Since I was downvoted, I like to point out again: Don't do this unless you know exactly what you are doing. In my case, almost the entire application was written in client-side javascript, and on those few occations where postbacks occurred, I always used the Request.Form collection to retrieve the values. Do not use server-side controls for anything but simple rendering if you do this.

like image 103
erikkallen Avatar answered Sep 29 '22 15:09

erikkallen