本文介绍了通过`EnableViewState`和`ViewStateMode`在asp.net 4.0减少viewstate-困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图清理已启用ViewState中无处不在旧的ASP.NET WebForms的网站。这是一个性能问题 - 巨大的viewstates造成明显的延迟提交。但大多数的形式并不真正似乎需要的ViewState除了一些复杂的控制和放大器;表单数据。即使没有输入控件的形式,虽然,产生大viewstates因为我想asp.net是存放各种metatdata约每一个服务器控件。但是,可见性状态,等等,所以我觉得我可以消除很多是所有受控于code。

I'm trying to clean up an older ASP.NET WebForms site that has ViewState enabled everywhere. This is a performance issue - huge viewstates cause noticeable submit delays. But most of the forms don't really seem to need ViewState except for some complex control & form data. Even forms with no input controls, though, generate big viewstates because I guess asp.net is storing all kinds of metatdata about every single server control. But the visibility state, etc, is all controlled in code so I think I can eliminate a lot.

这是pretty繁重至的EnableViewState =false的添加到每个单独的控制(在一个页面,并在code创建)不需要它,所以我想在页面/控制水平来禁用它,并选择性地启用它需要它的东西。 (是的,我知道这是有风险的,但也有真的只是一对夫妇的大单和一对夫妇的模板,如果处理会带来很大的区别)。

It's pretty onerous to add EnableViewState="false" to every single control (in a page and created in code) that doesn't need it, so I'm trying to disable it at a page/control level, and selectively enable it for things that need it. (Yes, I realize this is risky, but there are really just a couple big forms and a couple templates that, if addressed would make a big difference).

下面就是我不太得到。

如果一个控件或页面有的EnableViewState =false的它的<%...%GT; 描述符,或者它的标签,其中它是在父页面,一切符创建的,因为在code添加任何ViewState的数据不起作用。所以,我似乎能够把它留在控制层启用,但设置的EnableViewState 为false,在每个容器包装的控制,然后设置 ViewStateMode =真(其中ovverrides了),在每个控制水平。

If a control or page has EnableViewState="false" it its <%.. %> descriptor, or in its tag where it is created in the parent page, everything breaks, because any ViewState data added in code doesn't work. So I seem to be able to leave it enabled at the control level, but set EnableViewState to false for a wrapper control in each container, and then set ViewStateMode=true (which ovverrides that) at a per-control level.

什么我没有变是发生了什么时:

What I'm not getting is what happens when:

ViewStateMode = ViewStateMode.Enabled
的EnableViewState = FALSE 为包含其他控件的控件。

ViewStateMode = ViewStateMode.Enabled andEnableViewState = false for a control that contains other controls.

有关控制。可以内控制与 ViewStateMode 仍处于启用状态?基本上,它设置有当他们发生冲突一锤定音?

for a control. Can inner controls be enabled still with ViewStateMode? Basically, which setting has the final word when they conflict?

有关每个容器,我希望能够在一个包装控件禁用一切,但仍然保证:

For each container, I want to be able to disable everything in a wrapper control but still ensure that:

1)在code工作的ViewState设置

2)的 ViewState是为所有控件默认情况下禁用

3)的我可以选择性地启用ViewState中的子控件。

这似乎是混杂。如果我在母版页的包装控制被设置为的EnableViewState =false的,但然后设置一个子控件为 ViewStateMode =启用 ,它打破。根据MS,ViewStateMode应该凌驾任何外ViewState的设置,但它似乎并没有工作。

This seems to be confounding. If I have a wrapper control in the master page that is set to EnableViewState="false", but then set a subcontrol to ViewStateMode="Enabled", it breaks. According to MS, ViewStateMode should supercede any outer ViewState settings, yet it doesn't seem to work.

推荐答案

由于每的 MSDN文章 ViewStateMode 才有意义时,的EnableViewState =真正的。为了达到你想要什么,你必须离开 ViewStateMode =已启用上的控制,然后敷主一身的子控件在 ASP:占位符 ViewStateMode =已禁用。这样的话,你仍然可以操纵的ViewState在codebehind,但没有孩子的控制将有ViewState的(除非你明确地设置有它的那些通过 ViewStateMode =已启用)。

As per the MSDN article on ViewStateMode, ViewStateMode only has meaning when EnableViewState="true". To achieve what you want, you'll have to leave ViewStateMode="Enabled" on the control, and then wrap the sub-controls of the main one in an asp:Placeholder that has ViewStateMode="Disabled". That way, you can still manipulate ViewState in the codebehind, but no child control will have ViewState (except the ones you explicitly set to have it via ViewStateMode="Enabled").

这篇关于通过`EnableViewState`和`ViewStateMode`在asp.net 4.0减少viewstate-困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:43