Hi
ASP.NET controls automatically retain their data when a page is sent
to the server by a user clicking a submit button. Microsoft calls this
persistence of data
view state. In the past, developers would
have to hack a way to remember the item selected in a drop-down menu or
keep the contents of a text box, typically using a hidden form field.
This is no longer the case; ASP.NET pages, once submitted to the server
for processing, automatically retain all information contained within
text boxes, items selected within drop-down menus, radio buttons, and
check boxes. Even better, they keep dynamically generated tags,
controls, and text. Consider the following ASP page, called
sample.asp:
<html>
<head>
<title>Sample Page using VBScript</title>
</head>
<body>
<form method="post" action="sample.asp">
<input type="text" name="txtName"/>
<input type="Submit" name="btnSubmit" text="Click Me"/>
<%
If Request.Form("txtName") <> "" Then
Response.Write(Request.Form("txtName"))
End If
%>
</form>
</body>
</html>
The equivalent page in ASP.NET, ViewState.aspx, demonstrates data persistence using view state:
ViewState.aspx
<html>
<head>
<title>Sample Page using VB.NET</title>
<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
lblMessage.Text = txtName.Text
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:TextBox id="txtName" runat="server" />
<asp:Button id="btnSubmit" Text="Click Me" OnClick="Click"
runat="server" />
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>
ViewState.aspx
<html>
<head>
<title>Sample Page using C#</title>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
lblMessage.Text = txtName.Text;
}
</script>
</head>
<body>
<form runat="server">
<asp:TextBox id="txtName" runat="server" />
<asp:Button id="btnSubmit" Text="Click Me" OnClick="Click"
runat="server" />
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>
In this case, the code uses ASP.NET controls with the runat="server"
attribute. As you can see in Figure 2.2, the text from the box appears
on the page when the button is clicked, but also notice that the data
remains in the text box! The data in this example is preserved because
of view state
ASP.NET supports view state. When a page is submitted, the information within the controls is preserved in the HTML Hidden fields.
You can see the benefits of view state already. But where is all that
information stored? ASP.NET pages maintain view state by encrypting the
data within a hidden form field. View the source of the page after
you've submitted the form, and look for the following code:
<input type="hidden" name="__VIEWSTATE" value="dDwtMTcyOTAyO
DAwNzt0PDtsPGk8Mj47PjtsPHQ8O2w8aTwzPjs+O2w8dDxwPGw8aW5uZXJodG
1sOz47bDxIZWxsbyBXb3JsZDs+Pjs7Pjs+Pjs+Pjs+d2wl7GlhgweO9LlUihS
FaGxk6t4=" />
This is a standard HTML hidden form field with the value set to the
encrypted data from the form element. As soon as you submit the form
for processing, all information relevant to the view state of the page
is stored within this hidden form field.
---------------
Hope this helps
SAN