Minimizing viewstate- confused by `EnableViewState` and `ViewStateMode` in asp.net 4.0

你说的曾经没有我的故事 提交于 2020-01-21 01:08:44

问题


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.

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

Here's what I'm not quite getting.

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 and EnableViewState = false for a control that contains other controls.

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) ViewState settings in code work, and

2) ViewState is disabled for all controls by default, and

3) I can selectively enable ViewState for subcontrols.

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.


回答1:


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



来源:https://stackoverflow.com/questions/5485850/minimizing-viewstate-confused-by-enableviewstate-and-viewstatemode-in-asp-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!