Friday, August 22, 2014

Explain viewstate in asp.net OR Viewstate and its advantage or disadvantages

Introduction: In this article today I will explain ViewState.

Description:


Viewstate is used to preserve the state of page and controls on page postback. It remains turn on by default. Once we store the values in viewstate, we can access these values throughout the page. I can only store the values for same page. Viewstate is stored on page in hidden fields itself in Base 64 encoded format. We commonly use Viewstate to store small data.

Advantages:
It does not require server resources
Easy to implement.
Viewstate can be easily enabled or disabled.
Viewstate ensures security because it stores in encoded format
It automatically retains the state of page.

Disadvantages:
Store values only on same page.
If we store large amount of data on page then page load might cause a problem.
It does not support the mobile devices.

Example:
Add a webform to project and design the page mention below:

<fieldset style="width:400px;height:auto;">
            <legend>ViewState Example:</legend>
    <table>
        <tr><td>Name:</td><td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
        <tr><td><asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" /></td><td><asp:Button ID="btncheck" runat="server" Text="Check Viewstate" OnClick="btncheck_Click" /></td></tr>
    </table>
            </fieldset>

IN C#:
protected void btnsubmit_Click(object sender, EventArgs e)
        {
            ViewState["name"] = txtname.Text.ToString();
            txtname.Text = string.Empty;
        }

        protected void btncheck_Click(object sender, EventArgs e)
        {
            if (ViewState["name"] != null)
            {
                txtname.Text = ViewState["name"].ToString();
            }
        }

IN VB:
  Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        ViewState("name") = txtname.Text.ToString()
        txtname.Text = String.Empty
    End Sub

    Protected Sub btncheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncheck.Click
        If Not ViewState("name") Is Nothing Then
            txtname.Text = ViewState("name").ToString()
        End If
    End Sub
Explain viewstate in asp.net OR Viewstate and its advantage or disadvantages

Disable or Enable viewstate:
We can disable or enable viewstate on control level or page level. By default viewstate is enable. To disable viewstate at page level by adding EnableViewState="false" with page directive:

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" %>

Disable viewstate for single control:

txtname.EnableViewState = false;

To enable viewstate property for single control:

txtname.EnableViewState = true;

Viewstate security:
To make viewstate secure there are two options:

1.      We can do it by adding EnableViewStateMac="true" with page directive.

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" EnableViewStateMac="true" %>

2.      Encrypt the data by adding ViewStateEncryptionMode with page directive. It has 3 following options:
a)      Always: it encrypt the data always
b)      Never: It never encrypts the data.
c)      Auto: It encrypts the data if any control request for it

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" EnableViewStateMac="true" ViewStateEncryptionMode="Always" %>

Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

2 comments:

  1. Excellent Example...Keep posting..Good Luck and thanks for your posts.

    ReplyDelete
    Replies
    1. Thanks for appreciation. keep reading for more valuable article.

      Delete