- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
来自 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/
如何使用 HttpContext 访问当前页面的 ViewState 我有一个需要实现的 ViewStateUtil 类: public static T GetViewState(ViewS
我们最近将网站迁移到 .NET 4.5,其中一项更改是将 targetFramework="4.5"添加到 httpRuntime 以选择加入 ASP.NET 4.5 行为。一切都按预期工作,直到我们
来自 Microsoft 的页面 Understanding ASP.NET View State : in the instantiation stage of the page life cycl
我在我的 ASPX 页面上禁用了 ViewState: 我有以下类(class): public class DashboardUpdatePanel : UpdatePanel { pub
我同意 the best way to shorten the ViewState is to disable it在不需要它的 ASP.Net 控件上。从一开始就保持小规模是一个好习惯。 我还听说它
我正在解决我们应用程序的一些安全问题,我们将 ViewStateEncryptionMode 设置为 true(在 web.config 中)。我注意到仍然有一个 _VIEWSTATE 字段,现在在
Asp.Net 有一些选项可以影响页面的 ViewState 生成方式(加密、添加 mac、ViewStateUserKey)。 我想自己做,不是基于配置,而是基于我自己的使用其他算法进行序列化和加密
我对如何在 C# 中使用 ViewState 感到困惑,例如使用的好处是什么: ViewState["VST"]="Value1"; Lable1.Text= ViewState["VST"].ToS
背景: 我有一个页面,其中有各种使用 JavaScript 处理的客户端事件。我想将其中一些事件与服务器端事件“同步”,或者以其他方式从代码隐藏调用函数。 我想用 JavaScript 调用的一些函数
我有一个带有 primefaces 3.5 的 JSF 2.2.0-m12 应用程序。由于我将应用程序从 JBoss 7.1.1 移植到 Tomcat 7.0.39,重新加载页面后输入字段(输入文本、
我正在编写一个程序来解码编辑并在 python 中重新编码 ViewState。我已经成功地找到了 View 状态并解码了 base64 编码的字符串,但在尝试反序列化时遇到了困难。有没有办法使用 p
我可以尝试将一些数据保存到 ViewState,但收到此错误:Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerServerErrorExc
我在一个页面上有几个 Div 标签,它们是动态的,即根据某些标准,它们对用户可见或不可见。我想将它们添加到页面的 View 状态,以便在回发时它们不会再次隐藏,我该怎么做? 最佳答案 我只会使用 AS
抓这个! 我已经用谷歌搜索了这个。1. 假设我有一个包含几个文本框的网络表单,ID 为 textbox1、textbox2、textbox3。他们都启用了 View 状态。我在这些文本框中放置了一些值
代码隐藏中有一些 View 状态的东西,然后有一个更新面板。当我将其添加到母版页引用的子页面时,它破坏了我之前从用户控件中获得的所有以前可用的功能。 child 掌握后面的页面代码 protected
假设我有一个对象集合 List collection = new List(); 以及将插入这些对象的字典 Dictionary> sections = new Dictionary>(); // e
我编写了一些网页,这些网页使用需要 ViewState 值的常见例程。 能否将 ViewState 传递到函数中? 如果是,怎么做到的? 如果没有,我还能如何从类中的方法访问它们? 最佳答案 这是关键
问题是,我需要在 CallBack 请求期间保留 ViewState。 我知道当请求是 CallBack 时,ASP.NET 默认情况下不会保存页面状态。所以,我需要一个替代方案来让它工作。 ASP.
我有一个 List 类型的列表.我想将它存储在 ViewState 中。如何做到这一点? private List JobSeekersList { get; set; } 最佳答案 基本上你只需要使
我编写了一个 ASP.NET 应用程序,它在服务器的 IIS7 中运行。如果我在浏览器中打开此网络表单并向我显示站点代码,我会看到... 我有很多控件,如按钮、标签、文本框和 ListView。我尝试
我是一名优秀的程序员,十分优秀!