Tuesday, 6 August 2013

ASP.NET ViewState example in VB.NET or C# || How to use ViewState ASP.NET || Working with ViewState in ASP.NET

Hi friends, in this article I will explain about ASP.NET ViewState example in VB.NET or C# || How to use ViewState ASP.NET || Working with ViewState in ASP.NET.
I explained in the previous article about state management on Cookies and Sessions.
  • ViewState is the mechanism that allows state values to be preserved across page postbacks.
  •  I already explained about state management using cookies and sessions.
  • Viewstate is also the mechanism used to store the values across the page postbacks.
  • Maintaining the viewstate is the default for ASP.NET web pages.
  • If you don’t want to maintain the ViewState then include the directive <%@ Page EnableViewState="false" %> at the top of an .aspx page or add the attribute EnableViewState="false" to any control.

I will explain the viewstate with small example.
Take the below controls in your .aspx page.

In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET ViewState example: how to use ViewState in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">ASP.NET ViewState example</h2>   
        <asp:Label
            ID="Label1"
            runat="server"
            ForeColor="DarkGreen"
            Font-Size="Large"
            >
        </asp:Label><br />
        <asp:Button
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            ForeColor="Red"
            Font-Bold="true"
            Text="Show Button Click Status"
            />&nbsp;<br/>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>
After that write the following code in button click and page load.

In VB.NET:
Imports System
Partial Class viewstate
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clickCounter As Integer
        If ViewState("ClickCounter") Is Nothing Then
            clickCounter = 1
        Else
            clickCounter = CInt(ViewState("ClickCounter")) + 1
        End If
        ViewState("ClickCounter") = clickCounter
        Label1.Text = "Button clicked " & clickCounter & " times."
      
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            Label2.Text = "First Time"
        Else
            Label2.Text = "After Postback"
        End If
    End Sub
End Class


In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class viewstate : System.Web.UI.Page
{

      protected void Button1_Click(object sender, System.EventArgs e)
      {
            int clickCounter = 0;
            if (ViewState["ClickCounter"] == null) {
                  clickCounter = 1;
            } else {
                  clickCounter = Convert.ToInt32(ViewState["ClickCounter"]) + 1;
            }
            ViewState["ClickCounter"] = clickCounter;
            Label1.Text = "Button clicked " + clickCounter + " times.";

      }

      protected void Page_Load(object sender, System.EventArgs e)
      {
            if ((!IsPostBack)) {
                  Label2.Text = "First Time";
            } else {
                  Label2.Text = "After Postback";
            }
      }
    Public viewstate()
      {
            Load += Page_Load;
      }
}

Viewstate is stores that even postback. The above shows how data stored while postbacks.When you run the page first time (means it’s not postback) label2 shows First Time.  When you click on the button it will be postbacked and label2 shows After Postback and count will be decreased.
The output is as follows.
When you run the page postback
After clicking the button


 I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.