Keeping ViewState in SessionPageStatePersister

I want to keep viewstate of a specific page in session but following code block does not help me, what might I be missing?

So here is the code-behind file content of my page;

    public partial class ConfigurationEditorWebForm : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override bool VerifyAccess()
        {
            return true;
        }

        protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(this);
            }
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Note that even after you move the page state to view state, you will still see a __Viewstate element on your pages.

Also, I believe that this solution may be more correct than the others offered, as it handles the back button a bit more elegantly. When you store the ViewState in a hidden variable, it becomes part of the HTML, and is therefore available if the user clicks the back button 2 or 3 pages and continues with what he was doing. Blindly trashing the Viewstate with each page request will prevent the back button from behaving the way the user is expecting.

However, it would be remiss to note that this is probably not a great idea in the first place. I would suggest doing at least two things first:

First, make sure that you have turned off Viewstate on all of the controls that don’t need it (which will probably be most of them). You can do this by setting ‘IsViewstateEnabled’ to false.

Second, turn on http compression on your webserver. Even if you have a reasonably sized viewstate, the total page weight (when compressed – not as viewed in your browser) should be relatively small.

Hope that helps!

Method 2

What you might be seeing in the ViewState hidden field is the ControlState which is stored differently from the “normal” ViewState.

To also store the ControlState in session, add the following entry to the web.config file:

  <system.web>
    <browserCaps>
      <case>
        RequiresControlStateInSession=true
      </case>
    </browserCaps>
  </system.web>

Check this page for more.

Method 3

The important part of the code is

protected override PageStatePersister PageStatePersister
{
    get
    {
        return new SessionPageStatePersister(this);
    }
}

This overrides the normal page state persister and provides one that persists the page data to the Session instead of the ViewState.

Method 4

My answer is probably not what you want but as a last resort you might have to do something like this.

I applied the reflection lock described at https://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=240686 and built my own in-process session manager and stored all session data in there.

After finding the WYSIWYG builder generated poor HTML I switched to manually writing all my HTML with Response.Write.

Presto no more problems. A lot of work but it was worth it.

EDIT: no fair downvoting below 0 because you don’t like the idea of disregarding all of web forms. When tools don’t work for me I don’t use them. If tools don’t work for you and you cannot get them working, you shouldn’t use them either.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x