What is view state in asp.net

In Asp.Net ViewState is used for maintain the state of asp.net 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 very simple example.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View State in asp.net </title>
<form id="form1" runat="server">
<td>View State Data:</td><td><b><asp:Label ID="lblString" runat="server"/></b></td>
<tr><td></td><td> <asp:Button ID="btnClick" runat="server" Text="Get Data form ViewState"

After that write the following code in button click

using System;
protected void Page_Load(object sender, EventArgs e)
string str = "Welcome to Programming Tutorial";
ViewState["SimpleValue"] = str;

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

Leave a Reply

Your email address will not be published. Required fields are marked *