Why does the page keeps its previous state if EnableViewState is set to false?

Yanıt Why does the page keeps its previous state if EnableViewState is set to false?

  • 27 Temmuz 2012 Cuma 11:33
     
      Kod İçerir

    A very basic question, in a very simple scenario:

    In trying to understand the Web Forms model page lifecycle and the View State mechanism implementation of a way to keep page states,

    I have the following scenario: a blank page, with a text box and a button.

    Why is it that if I set the page EnableViewState property to False, the contents I put in the text box are kept after hitting the button and thus generating a postback? The only way, as far as I understand, that that could happen is if the server side could have any mechanism of pointing out - after the creation of all the page objects, as defined in the aspx file - from scratch, at every new round trip to the server - whether some of this objects have had their properties changed - and in such cases apply them after the Page initialization stage completes.

    However, after reading aware of the confusion about EnableViewState and ViewStateMode, I understand that... ViewStateMode is subjugated to EnableViewState, and if the latter is set to false, no object in the page is allowed to store its state.

    With that being said - is it not exactly what is happening in the scenario below? So where does the content of the text box could possibly come from !?

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" EnableViewState="false"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        </div>
        </form>
    </body>
    </html>
    

Tüm Yanıtlar