ASP.net State Engine

download ASP.net State Engine

of 5

Transcript of ASP.net State Engine

  • 7/28/2019 ASP.net State Engine

    1/5

    ViewState

    ViewState allows ASP.NET to repopulate form fields on each postback to the server, making sure that a

    form is not automatically cleared when the user hits the submit button. All this happens automatically,

    unless you turn it off, but you can actually use the ViewState for your own purposes as well. Please keep

    in mind though, that while cookies and sessions can be accessed from all your pages on your website,ViewState values are not carried between pages. Each control is responsible for storing its own state,

    which is accomplished by adding its changed state to its ViewState property. The ViewState property

    is defined in the System.Web.UI.Control class, meaning that all ASP.NET server controls have this

    property available.

    Here is a simple example of using the ViewState to carry values between postbacks:

    ViewState



    Name retrieved from ViewState:

    And the CodeBehind:using System;using System.Data;using System.Web;

    public partial class _Default : System.Web.UI.Page{

    protected void Page_Load(object sender, EventArgs e){

    if(ViewState["NameOfUser"] != null)

    NameLabel.Text = ViewState["NameOfUser"].ToString();else

    NameLabel.Text = "Not set yet...";}

    protected void SubmitForm_Click(object sender, EventArgs e){

    ViewState["NameOfUser"] = NameField.Text;NameLabel.Text = NameField.Text;

    }

  • 7/28/2019 ASP.net State Engine

    2/5

    }

    Try running the project, enter your name in the textbox and press the first button. The name will

    be saved in the ViewState and set to the Label as well. No magic here at all. Now press the

    second button. This one does nothing at all actually, it just posts back to the server.

    As you will notice, the NameLabel still contains the name, but so does the textbox. The first thing is

    because of us, while the textbox is maintained by ASP.NET it self. Try deleting the value and pressing thesecond button again. You will see that the textbox is now cleared, but our name label keeps the name,

    because the value we saved to the ViewState is still there!

    ViewState is pretty good for storing simple values for use in the form, but if you wish to save more

    complex data, and keep them & transfer them from page to page, you should look into using cookies or

    sessions.

    Maintaining the ViewState

    When a form is submitted in classic ASP, all form values are cleared. Suppose you have submitted a form

    with a lot of information and the server comes back with an error. You will have to go back to the form

    and correct the information. You click the back button, and what happens.......ALL form values are

    CLEARED, and you will have to start all over again! The site did not maintain your ViewState.

    When a form is submitted in ASP .NET, the form reappears in the browser window together with all

    form values. How come? This is because ASP .NET maintains your ViewState. The ViewState indicates

    the status of the page when submitted to the server. The status is defined through a hidden field placed

    on each page with a control. The source could look something like this:

    Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you want to NOT maintain

    the ViewState, include the directive at the top of an .aspx page

    or add the attribute EnableViewState="false" to any control.

    For more on ViewState, read this:

    http://msdn.microsoft.com/en-in/library/ms972976.aspx

    http://msdn.microsoft.com/en-in/library/ms972976.aspxhttp://msdn.microsoft.com/en-in/library/ms972976.aspxhttp://msdn.microsoft.com/en-in/library/ms972976.aspx
  • 7/28/2019 ASP.net State Engine

    3/5

    Look at the following .aspx file. It demonstrates the "old" way to do it. When you click on the

    submit button, the form value will disappear:

    Your name:

    Here is the new ASP .NET way. When you click on the submit button, the form value will NOTdisappear:

    Example

    Click view source to see that ASP .NET has added a hidden field in the form to maintain the

    ViewState

    Sub submit(sender As Object, e As EventArgs)lbl1.Text="Hello " & txt1.Text & "!"

    End Sub

    Your name:

  • 7/28/2019 ASP.net State Engine

    4/5

    The following diagram illustrates the sequence of events that transpire, highlighting why the change to

    the Label's Text property needs to be stored in the view state.

    What needs to be stored in the view state is any programmatic changes to the page's state. Forexample, suppose that in addition to this Label Web control, the page also contained two Button Web

    controls, a Change Message Button and an Empty Postback button. The Change Message Button has a

    Click event handler that assigns the Label's Text property to "Goodbye, Everyone!"; the Empty

    Postback Button just causes a postback, but doesn't execute any code. The change to the Label's Text

    property in the Change Message Button would need to be saved in the view state. To see how and when

  • 7/28/2019 ASP.net State Engine

    5/5

    this change would be made, let's walk through a quick example. Assuming that the HTML portion of the

    page contains the following markup:



    And the code-behind class contains the following event handler for the Button's Click event:

    private void btnSubmit_Click(object sender, EventArgs e){

    lblMessage.Text = "Goodbye, Everyone!";}

    The ASP.NET view state imposes two performance hits whenever an ASP.NET Web page is

    requested:

    1. On all page visits, during the save view state stage the Page class gathers the collectiveview state for all of the controls in its control hierarchy and serializes the state to a base-

    64 encoded string. (This is the string that is emitted in the hidden __VIEWSTATE formfiled.) Similarly, on postbacks, the load view state stage needs to deserialize the persisted

    view state data, and update the pertinent controls in the control hierarchy.

    2. The __VIEWSTATE hidden form field adds extra size to the Web page that the client mustdownload. For some view state-heavy pages, this can be tens of kilobytes of data, which

    can require several extra seconds (or minutes!) for modem users to download. Also, whenposting back, the __VIEWSTATE form field must be sent back to the Web server in theHTTP POST headers, thereby increasing the postback request time.