• What is View State and how it works in ASP.NET

    by  • April 17, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET, Microsoft VB.NET • 0 Comments

    View state is a state management technique in ASP.NET which works for a single page. It means ASP.NET framework uses view state to preserve page and control values during round trips. It is used automatically, unless you turn it off, by ASP.NET framework to keep information between postbacks of a page. You can also use view state to store information but it can only be used for one page and cannot be accessed by another page.So view state values are not carried between pages. View state is helpful when we need to repopulate form fields on each postback and we want to make sure that a form is not automatically cleared when the user hits the submit button.

     View state is a secure and easy way to retain information between postbacks for a single page. The state of the page and control values are serialized into base64 encoded strings when the HTML markup is rendered and this information is then placed into the hidden field called view state. This technique was not available in classic ASP so we were unable to retain the page information at that time.

    Here is the simple example how view state works in ASP.NET.

    Create a website in visual studio and add a webformin C# or VB.NET

    Add following controls in the webform

    Now see code for submit button click event

    C#

    VB.NET

    Simply store your control values in ViewState

    Write following code for restore button click

    C#

    VB.NET

    Now get your control values from ViewState and assign these values to textboxes.

    Download161 downloads



    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply