c# - 何时可以覆盖与 ViewState 关联的功能并使其使用 ControlState?

标签 c# asp.net viewstate web-controls

ControlState vs ViewState

我正在使用 RadListBox。 RadListBox 具有以下默认设置为 True 的属性:PersistClientChanges

此属性的功能取决于 ViewState。也就是说,当客户端进行更改时,更改将存储到控件的 ViewState 模型中——然后在下一次回发期间解析 ViewState,更改“重新运行”并应用于控件服务器端.

不幸的是,我的页面上没有启用 ViewState,而且我也没有真正尝试启用它。话虽这么说,我想要这个功能。所以,我正在考虑其他选择。

MSDN 文档指出“仅对少量关键数据使用控件状态,这些数据对于跨回发的控制至关重要。不要使用控件状态作为 View 状态的替代方法。”

我想知道:

  • 什么算作少量关键数据?一个 bool ?一小部分元素?少于 10000 个数据点??
  • 我应该注意哪些问题?这对我来说是一条愚蠢的道路吗?等等。

任何其他关于正确使用 ControlState 的信息都会很好。谢谢。

最佳答案

首先我必须承认,我从未使用过ControlState 来跨回发持久化值。 但是,创建供其他开发人员使用的控件的控件开发人员可能希望使用它来确保功能,即使 ViewState 被禁用也是如此。否则禁用会导致不正确的行为或不明显的错误。

所以在我看来,如果您通常使用 ViewState,您会使用 ControlState,但您必须确保当 ViewState 为禁用。 在这种情况下,“不要将控件状态用作 View 状态的替代方案” 这句话是错误的,因为您也应该只对少量的数据使用 ViewState必须在回传中持久化。

一个例子来阐明我的意思:

假设您想创建一个继承 GridView 的自定义控件。您可以将数据存储在 ViewState 中以在回传中维护值。您应该允许在您的控件上禁用 ViewState 而不会出现任何问题,您不应该依赖它已启用的事实,因为使用您的控件的开发人员可能希望减少网络流量。但是您可以提供存储在 ControlState 中的其他属性,例如您认为的 SortDirectionPageIndexDeleteButtonText 等很重要并且是单个值,因此比存储所有 GridViewRows 占用的资源更少。

编辑:根据你的实际问题:

您可以为某些控件启用 ViewState 而为其他控件禁用它,但您不能为整个页面禁用它并为此页面中的子控件启用它。

If you set turn page's ViewState off, then there is no way for you to enable ViewState for specific components. This is because ViewState is serialzed recursively, so when if the Page is not allowing ViewState, it will not serialize the ViewState for any of it's child controls. If you don't want to explicitly turn ViewState off on individual controls, but want to keep some controls ViewState aware, the best way would be writing a small utility method which turns ViewState off for all controls (using recursion or otherwise). Then enable ViewState for the controls that you would like to enable ViewState for. Alternatively, a middle ground and less forceful way may possible if controls are groups inside other container controls (such as Panel). You can disable ViewState for all controls inside a Panel by disabling ViewState of the Panel.

Enable ViewState for few controls and disable for others/page

编辑:这是为所有子控件启用/禁用 ViewState 的扩展 (VB.NET)

<System.Runtime.CompilerServices.Extension()> _
Public Sub EnableChildViewState(ByVal parentControl As System.Web.UI.Control, enable as Boolean) 
    If parentControl.HasControls Then
        For Each c As System.Web.UI.Control In parentControl.Controls
            c.EnableViewState = enable 
            EnableChildViewState(c, enable)
        Next
    End If
End Sub

你可以这样调用它:

Page.EnableChildViewState(False)
MyControl.EnableViewState = True

关于c# - 何时可以覆盖与 ViewState 关联的功能并使其使用 ControlState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7878975/

相关文章:

c# - 如何在 ASP 成员资格提供程序中使用 GetAllUsers?

c# - Azure Web API 访问 : Which URL should I use, Web 服务 URL 或网关 URL?

asp.net - 多环境.NET Core

c# - XmlSerializer 如何排除空值但保留标记? C#

javascript - CryptoJS 解密 C# DES 加密文件失败

asp.net - 如何在文本框控件上使用 SetFocus?

C#:更改事件目录用户密码时出现代码错误

asp.net - 为什么我动态添加的控件在回发后会丢失它们的值?

internet-explorer - 此页面的状态信息无效,可能已损坏

AngularJS:ui-router使用新内容重定向成功和刷新范围的 View