aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies ViewState Example in C#, VB.NET

Nov 4, 2012

Here I will explain what is viewstate and uses of viewstate with example in using c# and


In previous posts I explained Create ContactUs Form, Detect Browser type in jQuery, Scroll to particular link when click on link, send mail with images using gmail in and many relating articles in, jQuery. Now I will explain what is viewstate and uses of viewstate with example in using c# and

What is ViewState?

ViewState is used to maintain the state of controls during page postback and if we save any control values or anything in viewstate we can access those values throughout the page whenever it required for that check below simple example

<html xmlns="">
<head runat="server">
<title>View State in Example</title>
<form id="form1" runat="server">
<td>ViewState Data:</td><td><b><asp:Label ID="lblString" runat="server"/></b></td>
<tr><td></td><td> <asp:Button ID="btnClick" runat="server" Text="Get ViewState Data"
Now add following namespaces in your codebehind

C# Code

using System;

After that write the following code in button click

protected void Page_Load(object sender, EventArgs e)
string str = "Welcome to Aspdotnet-Suresh Site";
ViewState["SampleText"] = str;

protected void btnClick_Click(object sender, EventArgs e)
lblString.Text = ViewState["SampleText"].ToString();

Partial Class VBViewStateCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim str As String = "Welcome to Aspdotnet-Suresh Site"
If ViewState("SampleText") Is Nothing Then
ViewState("SampleText") = str
End If
End If
End Sub

Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As EventArgs)
lblString.Text = ViewState("SampleText").ToString()
End Sub
End Class

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


rams said...

Thank u boss....for publishing this article...

Naga Sudheer said...

hi suresh i like your blog, could u please post event on Interface with real time example.

Anonymous said...

Thanks brother...

shubham M said...

very nice artical...thanx a lot

Anonymous said...

thanks man!

hash said...

hi suresh,

can we store gridview's data to the viewstate?

the data is coming on programatically(dynamic) binding of the gridview. Want this griedview data to export to the ms-excel.

can u help out on this?

rohit said...

i m rohit
thanks for posting this code

Mr.Kannone Lievien said...

hi suresh i want to set the viewstate property i.e. true or false through textbox and my coding for it is :

string str = TextBox2.Text;
bool option = Convert .ToBoolean(str );
// bool opt = bool.Parse(str);
if (TextBox2.Text != null)
Page.EnableViewState = option;
Page.EnableViewState.GetType() ;
if (Page.IsPostBack)
//string str = TextBox1.Text;
Label4.Text = "raj";
Label4.Text = "sorry";

is not working so please help me

vick1942 said...


hi suresh this is dev u done a good job ur blog is very usefull to a begginer and experiance people thank you very much

zeniya opi said...

Convert your VB programs to C# with over 99% accuracy with the VBConversions VB.Net to C# Converter, the most accurate VB.Net to C# Converter available. Free download available at to c#

Anonymous said...

in which situation we go for sessions and viewstate
please clarify the same .

Raju Kuppusamy said...

Hi Suresh

Your Article is too good, It's Really helpful to me.....

K Vijayakumar said...

Hi Suresh,
Can you explain about postback and its usage on a webpage ?

ajit agarwal said...

i want to create shop-cart where data is store in view state for multiple item selection at time.

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.