如何在asp.net中最小化页面的视图状态大小?

时间:2022-07-16 01:40:09

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

如何在asp.net中最小化页面的视图状态大小?请帮忙。

6 个解决方案

#1


19  

You have several options to reduce the ViewState:

您有几个选项来减少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.
  • 禁用ViewState以获取不需要它的控件(这是最有效的解决方案)。例如。如果您可以在服务器上缓存一些数据,那么您可以将任何数据绑定控件与每个请求重新绑定,而不需要在ViewState中保存所有内容。
  • Turn on HTTP compression on the server (IIS). This reduces the size of the page sent to the client, including the ViewState.
  • 在服务器(IIS)上打开HTTP压缩。这减少了发送到客户端的页面大小,包括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.
  • 压缩ViewState。这比HTTP压缩还有一个额外的优势:它还减少了PostBacks(发送回服务器的数据)的大小,因为ViewState总是在PostBack期间发送回服务器。对此有各种方法,例如如本博文所示。
  • 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).
  • 将ViewState存储在服务器上,而不是将其发送到带有页面的隐藏字段中。最简单的方法是使用SesionPageStatePersister,但还有其他解决方案将ViewState存储到磁盘而不是使用Session(请参阅此处)。

#2


4  

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.

减少服务器控件的数量(例如web / html控件),特别是那些你不需要的控件。请改用简单的HTML标记。

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

我已经看到太多的冗余表/行/单元Web控件的情况,正常的

, 和
会这样做。

#3


1  

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.

您不能最小化ViewState的大小。这是序列化/反序列化的ASP.NET。虽然您可以选择性地禁用不需要它的控件的ViewState。

#4


1  

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 ...

我选择将视图状态保存在数据库本身的服务器上,而不允许它在HTML中传递给客户端,这会使页面大小膨胀。您可以扩展HiddenFieldPageStatePersister并解决此问题。如果你愿意,我已经写了一篇详细的文章...

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

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

#5


0  

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.

您可以打开服务器上的压缩,以最小化通过网络传输的数据大小,或将viewState保存到磁盘,并且永远不会将其发送到客户端。

#6


0  

   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.

在生成大型viewstate值的页面后面的代码中添加上面的代码。这允许将viewstate存储在会话中。现在只应添加视图状态的一个键。

#1


19  

You have several options to reduce the ViewState:

您有几个选项来减少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.
  • 禁用ViewState以获取不需要它的控件(这是最有效的解决方案)。例如。如果您可以在服务器上缓存一些数据,那么您可以将任何数据绑定控件与每个请求重新绑定,而不需要在ViewState中保存所有内容。
  • Turn on HTTP compression on the server (IIS). This reduces the size of the page sent to the client, including the ViewState.
  • 在服务器(IIS)上打开HTTP压缩。这减少了发送到客户端的页面大小,包括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.
  • 压缩ViewState。这比HTTP压缩还有一个额外的优势:它还减少了PostBacks(发送回服务器的数据)的大小,因为ViewState总是在PostBack期间发送回服务器。对此有各种方法,例如如本博文所示。
  • 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).
  • 将ViewState存储在服务器上,而不是将其发送到带有页面的隐藏字段中。最简单的方法是使用SesionPageStatePersister,但还有其他解决方案将ViewState存储到磁盘而不是使用Session(请参阅此处)。

#2


4  

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.

减少服务器控件的数量(例如web / html控件),特别是那些你不需要的控件。请改用简单的HTML标记。

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

我已经看到太多的冗余表/行/单元Web控件的情况,正常的

, 和
会这样做。

#3


1  

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.

您不能最小化ViewState的大小。这是序列化/反序列化的ASP.NET。虽然您可以选择性地禁用不需要它的控件的ViewState。

#4


1  

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 ...

我选择将视图状态保存在数据库本身的服务器上,而不允许它在HTML中传递给客户端,这会使页面大小膨胀。您可以扩展HiddenFieldPageStatePersister并解决此问题。如果你愿意,我已经写了一篇详细的文章...

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

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

#5


0  

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.

您可以打开服务器上的压缩,以最小化通过网络传输的数据大小,或将viewState保存到磁盘,并且永远不会将其发送到客户端。

#6


0  

   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.

在生成大型viewstate值的页面后面的代码中添加上面的代码。这允许将viewstate存储在会话中。现在只应添加视图状态的一个键。