【问题标题】:Asp.net Page controls and viewstateAsp.net 页面控件和视图状态
【发布时间】:2016-09-15 04:41:25
【问题描述】:

如果我设置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>

【问题讨论】:

标签: asp.net viewstate


【解决方案1】:

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

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

【讨论】:

  • 我明白了我的理解存在缺陷的地方。谢谢。
【解决方案2】:

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

示例:考虑以编程方式设置背景色。在回发时,如果关闭视图状态,则 Textbox 控件的 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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多