what is view state management in c#
Introducation:
ViewState is a client-side state management technique in the ASP.NET page framework that stores page and control values between round trips. It can be used to
Store non-default control values
Store application data specific to a page
Keep values between postbacks without storing them in session state or in a user profile
ViewState has several advantages, including:
Simplicity and ease of use
Flexibility
Server-independent
Enhanced security
Easy to implement
Doesn't require server resources
Can be compressed or encoded
Define View State Management
View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.
Example View State Management:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<p>
UserName: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Password: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Restore" />
</p>
</form>
</body>
</html>
// Declaration of 'a' and 'b'
public string a, b;
protected void Button1_Click(object sender, EventArgs e)
{
// TextBox1 and TextBox2 values are assigned to the variables 'a' and 'b'
a = TextBox1.Text;
b = TextBox2.Text;
// After clicking on Button, TextBox values will be cleared
TextBox1.Text = TextBox2.Text = string.Empty;
}
protected void Button3_Click(object sender, EventArgs e)
{
// Values of variables 'a' and 'b' are assigned to TextBox1 and TextBox2
TextBox1.Text = a;
TextBox2.Text = b;
}
No comments:
Post a Comment