Prashant Jadhav

Prashant Jadhav

  • NA
  • 38
  • 14.3k

how does LoadViewState and LoadPostData work

Jan 30 2015 1:43 AM
i am trying to understand, how view state data flow from browser to server and  vice versa and how it get create, i have been struggling with this from few days.
here are those point which i understand from reading some articles.

 1. when page is requested an auto generated class is get created which has code 
    to add controls to which form control hierarchy.
 2. after hierarchy is created all controls properties assigned to them which       
    has been declaratively mentioned in .aspx file.
    
 3. since those propeties get assigned by declarative syntax.they don,t need to 
    get save in view state.
 4. view state only get create when any controls properties are get assigned in 
    code.

     these are those points which i read in the article as in this link

     As stated in this article, in the Load Postback Data stage of page,page 
assign post back data which is in the form of form fields, to the interesting
control by identifying their ID and by identifying implementation of IPostBackDataHandler .my question is, in this stage (load post back data) those values which are in the form of form fields get assign to controls property but according to this article. here it is
most of the properties of control use view state in background.does it mean when user enter or modify an control property, lead to create view state for that property of effected control.because when post back data get assign to any control property that property is internally creating view state.then that view state is serialize in view state to rendered page.
But here i have another doubt, what if i put an text box on a page.keep its view state mode disable.and allow user to enter the value in it.
now since user has entered the value and it will going in the form of form field and it will get assign to text property of text box control.but since text property use view state as it backing field will that form field value get save in view state and will get send as view state to the rendered page.but here i have disabled the view state mode for text control,now how can that text box can contain same entered value in it after round trip.
actually i tried this coding i took an text box kept view state false.and wrote javascript function to enter value in text box.which get execute on click of an button and then i took another button which just post back the page nothing else.when i click this button after entering text in text box,i still found same text in text box.so this text box is getting this text value either by view state or somehow its text property is getting its value in load post back data stage of page,because that value is still there in text box.but if this property is getting its value in post back stage and still not creating view state then how is this possible it like when property are assign in declarative syntax doesn't create view state.how can there be two version of text property where one is creating view state to persist data across post back and another is not creating view state like in declarative syntax.
here is code that i tried.

         <%@Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"   
           Inherits="_Default" %>
         <!DOCTYPE html PUBLIC "-//W3C//DT  
         XHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1
            transitional.dtd">

         <html xmlns="http://www.w3.org/1999/xhtml">
         <head runat="server">
         <title></title>
         <script type="text/javascript">
          function fun() {
            document.getElementById("TextBox1").value = "amit";
          }
    
          </script>
          </head>
          <body>
          <form id="form1" runat="server">
          <div>
    
        <asp:TextBox ID="TextBox1" runat="server" ViewStateMode="Disabled" 
            EnableViewState="False">prashant</asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" onClientClick="fun()" Text="Button" />
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label" ViewStateMode="Disabled"></asp:Label>
        <br />
    
    </div>
    </form>
</body>
</html>
code behind file code

   

         using System;
         using System.Collections.Generic;
         using System.Linq;
         using System.Web;
         using System.Web.UI;
         using System.Web.UI.WebControls;

         public partial class _Default : System.Web.UI.Page
           {
             protected void Page_Load(object sender, EventArgs e)
              {

              }
             protected void Button1_Click(object sender, EventArgs e)
              {
                Label1.Text = "hello guys";
              }
             protected void Button2_Click(object sender, EventArgs e)
              {

              }
             protected void Button3_Click(object sender, EventArgs e)
              {

              }
           }