Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 25 March 2015

Viewstate is a type of state management. It is a Page-Level State Management technique. Viewstate is used to preserve the state of page and controls on page postback(between round trips). It remains turn on by default.

 Once we store the value in viewstate, we can access this value throughout the page during any no of post back. Normally we cannot access these values across pages. Value of View state is stored on page (client side) in hidden fields. Viewstate are normally used to store small data.
Advantages:
1.   It does not require server resources
2.   Easy to implement.
3.   Viewstate can be easily enabled or disabled.
4.   Viewstate ensures security because it stores in encoded format
5.   It automatically retains the state of page.

Disadvantages:
1.   Store values only on same page.
2.   If we store large amount of data on page then page load might cause a problem. So it is recommended to create less no of view state on one page. Otherwise page will be heavy.
3.   It does not support the mobile devices.

Explanation with example:
Design Section:
   <fieldset style="width:320px;height:auto;">
            <legend><strong>ViewState Example:</strong></legend>
    <table>
        <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
        <tr><td>Enter Name:</td><td><asp:TextBox ID="txtname" runat="server" Width="126px"></asp:TextBox></td></tr>
        <tr><td><asp:Button ID="btnsubmit" runat="server" Text="Save viewState" 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 AsSystem.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 AsSystem.EventArgs) Handles btncheck.Click
        If Not ViewState("name") Is Nothing Then
            txtname.Text = ViewState("name").ToString()
        End If
    End Sub


Disable or Enable viewstate:

BY default ViewState is enabled. We can disable or enable viewstate on control level or page level. To disable viewstate at page level by adding EnableViewState="false" in 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;
Categories: , ,

0 comments:

Post a Comment