Saturday, October 31, 2020

ViewState in ASP.NET

Web Application work on HTTP protocol. HTTP protocol is a stateless protocol, meaning it does not retain state between user requests.

   public partial class WebForm1 : System.Web.UI.Page

    {

        int count = 0;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                txtCount.Text = count.ToString();

            }

        }

        protected void btnClickMe_Click(object sender,EventArgs e)

        {

            count = count + 1;

            txtCount.Text = count.ToString();

        }

    }

Web forms live for barely a moment. When a reques is received

1. An Instance of the requested webform is created.

2. Events Processed

3. Generate the HTML & posted to the client

4. The webform is immediately destroyed


View State: 

   public partial class WebForm1 : System.Web.UI.Page

    {

    //Using view state

        int count = 0;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                txtCount.Text = count.ToString();

            }

        }

        protected void btnClickMe_Click(object sender, EventArgs e)

        {

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

            {

                count = (int)ViewState["clickCount"] + 1;

            }

            txtCount.Text = count.ToString();

            ViewState["clickCount"] = count;

        }

    }

Click button now, and the value gets incremented every time we click. So how is this possible now?

It's possible because, we are using the ViewState variable Clicks to preserve the data between 

requests. The ViewState data, travels with every request and response between the client and the web server.

ASP.NET Control And ViewState:

  public partial class WebForm1 : System.Web.UI.Page

    {

    //Using Asp.net Control

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                txtCount.Text = "0";

            }

        }

        protected void btnClickMe_Click(object sender, EventArgs e)

        {

            int clickCount = Convert.ToInt32(txtCount.Text) + 1;

            txtCount.Text = clickCount.ToString();

        }

    }

Upon clicking the button, the values gets incremented correctly as expected. This is possible because, txtCount is an asp.net server control, that uses ViewState internally, to preserve data across postbacks.

Because Web forms have very short lifetimes, APS.NET takes special steps to preserve the data entered in the controls on a web form. Data entered in controls is sent with each request and restored to controls in Page_Init. The data is these controls is then available in the Page_Load(), Button_Click(), and many more events, that  occurs after Page_Init() event.

ASP.NET Server Control Vs HTML Control

ASP.NET server controls retains state

HTML control, do not retain state post backs

An HTML control can be converted in ASP.NET server control, by adding runat="server" 

attribute in the HTML source as shown below.

<input type='text' id='txtbox1' runat='server'>

ViewState data is serialized into base64-encoded string, and is stored in Hidden input field.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="4g89g46o76lwybnSts7LPtWkHw2JV1HDLQ9uak59hOsQGMndYrk6HtBured/nV0rsjMbpb4LM07duPLdMoLI1wr9H++pnaPuXQVrdTArZiAd2wN1ds62QRzELhRGBr1l" />


No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...