views:

2124

answers:

5

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);
            }
        }
    }
A: 

Check this page:

http://blog.sb2.fr/post/2008/12/25/Storing-ViewState-On-Server-Instead-Of-Client-With-ASPNET.aspx

GrZeCh
My code is exactly the same but still I have a large viewstate
erdogany
+1  A: 

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.

Nick Berardi
I really hope this guy doesn't get bounty just for highlighting the problem.
Joshua
The problem is that everything he did is correct, unless he posts more, he may just be using the ViewState in the wrong way. I wanted to make sure a search user didn't stumble by and think what he did was wrong.
Nick Berardi
hi again, sorry for not being interested with question. First of all what I did is exactly same, as you can see from the code. There must me something else do to...
erdogany
+4  A: 

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!

Travis
+1  A: 

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.

Joshua
You might find it easier to maintain if you use the Html controls (from System.Web.UI.HtmlControls) and set EnableViewState to false on them. This will also be more consistent with what how other programmers work if anyone else needs to work on your site.
Travis
Still generates tags such as <BR /> which is incorrect in HTML 4.01.
Joshua
@Joshua I added a link to your answer, I hope you don't mind
Nick Berardi
+1  A: 

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.

M4N