Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
            }
        }
    }
like image 761
yusuf Avatar asked Dec 22 '08 12:12

yusuf


2 Answers

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.

like image 162
Joshua Avatar answered Oct 12 '22 01:10

Joshua


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!

like image 33
tsimon Avatar answered Oct 12 '22 01:10

tsimon