Asp.net Page控件和viewstate

时间:2011-04-21 18:38:14

标签: asp.net viewstate

如果我设置EnableViewState="false",那么在使用控件之后回发后也会保留输入数据,如

  • 文本框
  • 单选按钮
  • 复选框

所以我只是想了解内部事情,如果EnableViewState="false"那么当我们使用文本框,单选按钮复选框等控件时回发后如何维护输入数据,请讨论内部问题。

感谢。

这是我的aspx代码

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

              

    <asp:TextBox ID="TextBox1" runat="server" 
        style="position: relative; top: 0px; left: 0px;"></asp:TextBox>


    <asp:TextBox ID="TextBox2" runat="server" 
        style="position: absolute; top: 66px; left: 11px; z-index: 1;"></asp:TextBox>


<asp:RadioButton ID="RadioButton1" runat="server" style="position: relative" />

    <asp:CheckBox ID="CheckBox1" runat="server" style="position: relative" />

 &nbsp;<asp:Button ID="Button1" runat="server" Text="Button" />

 </div>

</form>

2 个答案:

答案 0 :(得分:4)

维护输入数据是因为浏览器会在每次回发时将输入数据发送到服务器。例如,每次有回发时,页面上的文本框都会将其文本发送到服务器。因此,text属性不需要存储在视图状态中,以便在回发中记住。

有关视图状态的更多信息,请查看以下文章:Understanding ASP.NET View State

答案 1 :(得分:0)

查看Server controls persist their state when EnableViewState is set to False

示例:以编程方式考虑背景色设置。在回发时,如果关闭了视图状态,则文本框控件的background color将丢失。但是,将保持控件的文本值。

注意:如果背景颜色直接设置在标记中而不是后面的代码中,那么它将一直存在。

<form id="form1" runat="server">
<asp:TextBox ID="Textbox1" runat="server"  EnableViewState="false"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" EnableViewState="false" />
</form>

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.Textbox1.BackColor = Color.Yellow;

    }
}

另请参阅

  1. Understanding ASP.NET View State

  2. View State for TextBox and other controls that implement IPostBackDataHandler

  3. How do I disable viewstate for a specific control?