How to minimize viewstate size of a page in asp.ne

2019-02-01 14:19发布

How to minimize viewstate size of a page in asp.net? Please help.

6条回答
贼婆χ
2楼-- · 2019-02-01 14:39

You have several options to reduce the ViewState:

  • Disable ViewState for controls that do not need it (this is the most effective solution). E.g. if you can cache some data on the server, then you can re-bind any databound controls with every request and it's not needed to save everything in ViewState.
  • Turn on HTTP compression on the server (IIS). This reduces the size of the page sent to the client, including the ViewState.
  • Compress the ViewState. This has an additional advantage over HTTP compression: it also reduces the size of PostBacks (data sent back to the server), since the ViewState is always sent back to the server during a PostBack. There are various approaches for this, e.g. as shown in this blog post.
  • Store the ViewState on the server instead of sending it in a hidden field with the page. The easiest way to do this is to use the SesionPageStatePersister, but there are other solutions which store the ViewState to disk instead of using the Session (see here for example).
查看更多
Luminary・发光体
3楼-- · 2019-02-01 14:40

I chose to save the view state on the server in a database itself and not allow it to be passed in the HTML to the client which bloats the page size. You can extend the HiddenFieldPageStatePersister and work around this. I have written a detailed article around this if you wish ...

http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/

查看更多
Bombasti
4楼-- · 2019-02-01 14:47

Most of the points are highlighted within the other answers. Here's one that might be helpful:

Reduce the number of server controls (e.g. web/html controls) especiall those you do not need. Use simple HTML markups instead.

I've seen too many cases of redundant Table/Row/Cell Web Controls where normal < table >, < tr > and < td > will do.

查看更多
beautiful°
5楼-- · 2019-02-01 14:47
   protected override PageStatePersister PageStatePersister
    {
        get
        {
            return new SessionPageStatePersister(this);
        }
    }

add the above code in the code behind of the page that generates large viewstate values. This allows storing the viewstate in the session. Only a key for the viewstate should be added now.

查看更多
We Are One
6楼-- · 2019-02-01 14:49

You cannot minimize the size of the ViewState. It's ASP.NET which serializes/deserializes. Though you could selectively disable ViewState for controls that don't need it.

查看更多
迷人小祖宗
7楼-- · 2019-02-01 14:59

You can turn on compression on the server to minimalize size of data transfered through the network or save viewState to disk and never send it to the client.

查看更多
登录 后发表回答