Aspdotnet-Suresh

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

Asp.net ViewState Example in C#, VB.NET

Nov 4, 2012
Introduction:

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

Description:

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 asp.net and many relating articles in asp.net, jQuery. Now I will explain what is viewstate and uses of viewstate with example in asp.net using c# and vb.net.

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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View State in asp.net Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>ViewState Data:</td><td><b><asp:Label ID="lblString" runat="server"/></b></td>
</tr>
<tr><td></td><td> <asp:Button ID="btnClick" runat="server" Text="Get ViewState Data"
onclick="btnClick_Click"/></td></tr>
</table>
</div>
</form>
</body>
</html>
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)
{
if(!IsPostBack)
{
string str = "Welcome to Aspdotnet-Suresh Site";
if(ViewState["SampleText"]==null)
{
ViewState["SampleText"] = str;
}
}
}

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


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
Demo



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

Get the latest Asp.net, C#.net, 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

15 comments :

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
http://www.compindiatechnologies.com/

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...

hi
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";
}
}
else
{
Label4.Text = "sorry";
}
}


is not working so please help me

vick1942 said...

THNX BUDDYU

www.Deve.Devender.blogspot.com 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 www.vbconversions.com.

vb.net to c#

Anonymous said...

hi,
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...

Sir,
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 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.