简体   繁体   English

Asp.net Page控件和viewstate

[英]Asp.net Page controls and viewstate

if i set EnableViewState="false" then also input data is maintain after postback at the time of using control like 如果我设置EnableViewState="false"那么在使用控件之类的回发后也会保持输入数据

  • textbox 文本框
  • radio button 单选按钮
  • checkbox 复选框
  • etc. 等等

So I just wanted to know bit internal things that if EnableViewState="false" then how input data is maintain after postback when we are using control like textbox, radio button checkbox etc. please discuss the internal issue. 所以我只想了解内部事情,如果EnableViewState="false"那么当我们使用文本框,单选按钮复选框等控件时,回发后如何维护输入数据,请讨论内部问题。

Thanks. 谢谢。

here is my aspx code 这是我的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>

Input data is maintained because the browser sends the input data to the server on every postback. 由于浏览器在每次回发时将输入数据发送到服务器,因此维护输入数据。 For example, the textbox on the page has its text sent to the server every time there is a postback . 例如, 每次有回发时,页面上的文本框都会将其文本发送到服务器。 Therefore, the text property does not need to be stored in view state in order to be remembered across postbacks. 因此,text属性不需要存储在视图状态中,以便在回发中记住。

For much more information on view state, check out this article: Understanding ASP.NET View State . 有关视图状态的更多信息,请查看本文: 了解ASP.NET视图状态

Take a look at Server controls persist their state when EnableViewState is set to False 看一下当EnableViewState设置为False时Server控件会保持其状态

Example : Consider backcolor setting programmatically. 示例 :以编程方式考虑背景颜色设置。 On postback, if viewstate is switched off, the background color of the Textbox control is lost. 在回发时,如果关闭了视图状态,则文本框控件的background color将丢失。 However, the text value of the control is maintained. 但是,保持控件的文本值。

Note: If the backcolor was set directly in markup rather than in code behind, it would have persisted. 注意:如果背景颜色直接在标记中设置而不是在后面的代码中设置,那么它将保持不变。

<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;

    }
}

Also refer 还请参考

  1. Understanding ASP.NET View State 了解ASP.NET视图状态

  2. View State for TextBox and other controls that implement IPostBackDataHandler 查看TextBox和其他实现IPostBackDataHandler的控件的State

  3. How do I disable viewstate for a specific control? 如何禁用特定控件的viewstate?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM