asp.net - 如何不保留不需要保留在 ViewState 中的 ViewState 内容?

标签 asp.net viewstate page-init

来自 Microsoft 的页面 Understanding ASP.NET View State :

in the instantiation stage of the page life cycle, the control hierarchy is created and those properties that are specified in the declarative syntax are assigned. Since these declarative properties are automatically reassigned on each postback when the control hierarchy is constructed, there's no need to store these property values in the view state.

这意味着如果您的 aspx 文件包含如下标记:

<asp:Label ID="Label1" runat="server" Font-Name="Verdana" Text="Hello, World!"></asp:Label>

不会必须在 ViewState 中保留的两件事是:

  • 字体名称:Verdana
  • 文本:Hello, World!

这两个属性不必保留在 ViewState 中,因为它们每次 都会设置。

另一个不必在 ViewState 中持久化的例子是在 aspx 标记文件中声明的项目:

<asp:DropDownList ID="DropDownList1" runat="server">
   <asp:ListItem>One</asp:ListItem>
   <asp:ListItem>Two</asp:ListItem>
   <asp:ListItem>Three</asp:ListItem>
</asp:DropDownList>

DropDownList 项:

  • 一个
  • 两个
  • 三个

不必保留在 ViewState 中,因为它们每次都被声明。

这就是我想做的。我希望不必在 ViewState 中保留的内容不在 ViewState 中保留。相反, 的事情必须保留在 ViewState 中,我希望它们保留在 ViewState 中。

再次来自 MSDN:

When the control hierarchy is built in the instantiation stage, the Label's Text property will be set to "Hello, World!" and its Font property will have its Name property set to Verdana. Since these properties will be set each and every page visit during the instantiation stage, there's no need to persist this information in the view state.

一个例子

除了不在标记中声明属性外,假设它们是以编程方式声明的:

Label1.Font.Name = "Verdana";
Label1.Text = "Hello, World!";

DropDownList1.Items.Add("One");
DropDownList1.Items.Add("Two");
DropDownList1.Items.Add("Three");

现在为了向 ASP.net 的魔力发出信号,这些属性总是 在那里,并且不需要在 ViewState< 中持久化/strong>,我将在页面初始化期间进行此更改:

protected void Page_Init(object sender, EventArgs e)
{
   //Set properties *before* PageLoad; before the viewstate is read
   //That way the properties will not have to be added to the viewstate
   Label1.Font.Name = "Verdana";
   Label1.Text = "Hello, World!";

   DropDownList1.Items.Add("One");
   DropDownList1.Items.Add("Two");
   DropDownList1.Items.Add("Three");
}

除非那行不通。尽管我在页面周期中足够早地设置了控件的属性,并且尽管我每次都设置了属性,但 ASP.NET 仍然认为它们需要保留在 View 状态

显然,我需要我的代码及时运行得更早。从 ASP.NET 认为它仍在读取标记的时候开始。

我如何保留ViewState不需要保留在ViewState中的内容?

后记

  • 为什么要这样做?只需将其放在标记中

    不能;不是当下拉项来自数据库时

  • 将控件的 ViewState 属性设置为 false

    不能;而不是在关闭 do 需要在 ViewState

  • 中保留的事情时

最佳答案

您基本上已经回答了您自己的问题。

You can't on a per-property basis decide what stays in and doesn't stay in ViewState, for stock asp.net controls.

如果您滚动自己的自定义控件,您可以有更多的粒度并通过自定义属性列表等精确控制哪些属性被存储,哪些不被存储,但是您必须重新创建轮子。

您可以继承内置控件(我怀疑是否有密封控件)并覆盖 View 状态和/或控件状态处理程序。老实说,这听起来像是一项收效甚微的艰巨任务。

我明白您为什么要问 - 为什么不比当前模型更高效。问题是 ASP.NET 从来都不是关于效率的,当然也不是轻量级的。它更多地是以牺牲性能和简单性为代价的快速开发 - 以 ajax UpdatePanel 设计模式为例。为了便于开发,它是一个完全臃肿的 ajax 实现。

关于asp.net - 如何不保留不需要保留在 ViewState 中的 ViewState 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208768/

相关文章:

c# - 如何从站点地图文件中获取当前页面标题?

ASP.NET - 数据源可以在 IIS 上配置,而不是驻留在 Web.Config 中吗?

asp.net-mvc - Mvc C#中ViewBag和ViewState的区别

jquery-mobile - jQuery Mobile - 绑定(bind)到 pageinit 事件

asp.net - 页面加载()或页面初始化()

asp.net - iOS 中的 anchor 链接

c# - 从代码隐藏(或前端)设置 mailto

asp.net - Gridview RowUpdating 事件未触发

c# - View 状态键的长度会影响 View 状态的大小吗?