asp.net - 我是否需要输入控件(如复选框、文本框等)的 View 状态?

标签 asp.net

EnableViewstate = false并且我进行回发时,我的文本框控件保留文本值,但标签控件不保留。我正在更改按钮的单击事件上的标签和文本框的文本。

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Message Changed";
    Label1.Text = "Message Changed";
}

我的页面还有另一个按钮用于回发。

此外,如果我不需要此控件的 View 状态,那么是否有所有不需要 View 状态的控件的列表?

最佳答案

所有实现 IPostBackDataHandler 的控件即使 ViewState 关闭,也会加载它们的值。请注意,当 ViewState 被禁用时,诸如 TextChanged 之类的事件和诸如 ForeColor 之类的其他属性将不起作用。

这是实现IPostBackDataHandler的控件列表

  • 复选框
  • 复选框列表
  • 下拉列表
  • HtmlInputCheckBox
  • HtmlInputFile
  • HtmlInput隐藏
  • HtmlInputImage
  • HtmlInputRadioButton
  • HtmlInputText
  • Html选择
  • HtmlTextArea
  • 图像按钮
  • 列表框
  • 单选按钮列表
  • 文本框

Why textbox persists data during postback even if View State set to off

Whenever a page is submitted or posted back to server, the entire form data is posted to the server as a collection with the request. The collection is in the form of NamedValue collection and this collection has the mapping with uniqueid of the control and the value of the control. You can read the data from the form collection by using the following code snippet

//Reading textbox value from the form collection 
string textboxvalue = Request.Form[textbox1.UniqueID];

ASP.NET uses this primitive to update the control’s value. ASP.NET uses IPostBackDataHandler for the controls that load the data from the form collection.

关于asp.net - 我是否需要输入控件(如复选框、文本框等)的 View 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11969245/

相关文章:

asp.net - 在线用户数

c# - Eval函数是C#

ASP.NET 应用程序错误覆盖 404?

asp.net - safari 浏览器无法处理基于 cookie 的 asp.net session

c# - 在 asp.net c# 4.0 中完全以编程方式创建 Gridview

javascript - Jquery 显示和隐藏控件

c# - 动态内容的最佳实践

asp.net - SQL 主键

javascript - 如何使用 C# 隐藏面板?

asp.net - 如何在 ASP.Net 中设置 Page.IsValid