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.
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. |
|||
|
|||
16 comments :
Thank u boss....for publishing this article...
hi suresh i like your blog, could u please post event on Interface with real time example.
Thanks brother...
very nice artical...thanx a lot
http://www.compindiatechnologies.com/
thanks man!
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?
hi
i m rohit
thanks for posting this code
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
THNX BUDDYU
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
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#
hi,
in which situation we go for sessions and viewstate
please clarify the same .
Hi Suresh
Your Article is too good, It's Really helpful to me.....
Hi Suresh,
Can you explain about postback and its usage on a webpage ?
Sir,
i want to create shop-cart where data is store in view state for multiple item selection at time.
Thank you so much sir....
Note: Only a member of this blog may post a comment.