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.
<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>
|
using System;
|
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();
}
|
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 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 Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
9 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