减少 ASP.NET 中 ViewState 大小的策略

发布于 2024-08-15 10:42:12 字数 204 浏览 9 评论 0原文

我在页面中使用了“n”个服务器控件。现在我正在进行性能调整,我注意到我的 ViewState 太大,这使得我的页面变慢。

我知道 ViewState 大小可以通过 Gzip 压缩。有关减少 ASP.NET 中的 ViewState 的任何其他建议。我不想在 IIS 中执行此操作,因为我的 Web 应用程序托管在共享服务器上。

I use 'n' number of server controls in my page. Now I am into performance tuning and I've noticed that my ViewState is too large and it makes my page slow.

I know ViewState size can be compressed by Gzip. Any other suggestions for reducing ViewState in asp.net. I don't want to do in IIS because my web application is hosted on a shared server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月棠 2024-08-22 10:42:12

假设视图状态的大小是“缓慢”的主要原因,我想建议您从更全面的方法来解决它。

您有“n”个服务器控件,是否需要所有“n”个数字都是服务器控件而不仅仅是纯 HTML?

假设您确实需要所有“n”个,那么所有这些都需要启用视图状态吗?

这是一篇好文章(如果您还没有阅读),它提供了更多见解:
VIEWSTATE 大小最小化

Assuming the size of viewstate is the main cause of the 'slowness', I would like to suggestion you tackle it from a more holistic approach.

You have 'n' number of server controls, do you need all 'n' numbers to be server controls and not just plain HTML?

Say you really need all 'n' of them, do all of them need to have viewstate enabled?

Here is one good article (if you haven't already read) which provides more insights:
VIEWSTATE size minimization

小鸟爱天空丶 2024-08-22 10:42:12

EnableViewState = false; 应该成为你的朋友。

假设您当前仅在需要的地方使用视图状态,您可以执行以下操作:

  1. Labels 切换为 Literals,特别是当您在模板中使用它们时。标签需要更多的视图状态。
  2. 尝试减少回发。较少的回发将使您需要更少地使用视图状态,因为您不需要重新加载整个页面。例如,使用 AJAX 调用并通过客户端操作写出数据。
  3. 使用会话或缓存来存储网格和数据密集型控件的大量数据。每次重新加载时,只需自己填充即可。

EnableViewState = false; should become your friend.

Assuming you currently are using viewstate ONLY where you need you can do the following:

  1. Switch Labels to Literals, especially if you are using them in templates. Labels take much more viewstate.
  2. Try do reduce postbacks. Less postbacks will make you need to use viewstate less as you will not need to reload the entire page. For example use AJAX calls and write out data via client side manipulation.
  3. Use session or caching to store large amounts of data for grids and data heavy controls. With each reload, just fill it yourself.
把梦留给海 2024-08-22 10:42:12

仅当您需要记住回发之间页面的状态时才应使用视图状态。它用于防止对数据库的额外访问。因此,如果您的控件不需要这样做,请使用 EnableViewState = False。如果页面上没有任何内容需要视图状态,您可以通过在 Page 标记内添加 EnableViewState = False 来禁用该页面的视图状态。

如果您的服务器负担得起,您可能希望将数据传输到会话中。如果出于安全需要(视图状态不应包含敏感数据),或者您的视图状态保存了大量数据,请执行此操作。请注意,默认情况下,会话 存储在服务器的内存中。因此,如果您预计有许多并发用户,则不希望对大数据过多使用此功能。但是,您可以更改会话的存储位置(即另一台服务器)。

Viewstates should only be used when you need to remember the state of the page between postbacks. It's used to prevent extra access to the database. So, if this isn't required in your control, use EnableViewState = False. If nothing on your page is going to need a viewstate, you can disable viewstates for that page by adding EnableViewState = False within the Page tag.

If your server can afford it, you may want to transfer data into Sessions. Do this if necessary for security (viewstate should not contain sensitive data), or if your viewstate is holding a large amount of data. Be careful as, by default, Sessions are stored in memory of the server. Therefore, you don't want to use this too much with large data if you're expecting many concurrent users. You can, however, change where the Session is stored (i.e. another server).

财迷小姐 2024-08-22 10:42:12

将以下代码添加到生成大视图状态值的页面。
或者,可以将其添加到母版页中,从而无需在每个页面上添加。下面的代码允许将视图状态存储在会话中。

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

Add the following code to the page which generates large viewstate values.
Alternatively, this can be added to master page to eliminate the need of adding on each page. The below code allows the viewstate to be stored in session.

        protected override PageStatePersister PageStatePersister
    {
        get
        {
            return new SessionPageStatePersister(this);
        }
    }
无语# 2024-08-22 10:42:12

您应该做的第一件事就是在不需要的地方关闭视图状态。检查控件并确定哪些控件绝对需要打开视图状态。

The first thing you should do is turn off viewstate wherever you don't need it. Examine the controls and determine which ones absolutely need viewstate turned on.

孤星 2024-08-22 10:42:12

正是我遇到的问题。我必须扩展 HiddenFieldPageStatePersister 并将视图状态保存在数据库中。我写了整篇文章来指导您。

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

Exactly the issue that I had. I had to extend the HiddenFieldPageStatePersister and save the viewstate in a database. I have written an entire article that will guide you.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文