Now I am showing you an example of what the problem is when we don't use view state
Step 1: Open Visual Studio 2012.
Step 2: Then click on "New Project" > "Web" >"ASP.NET Empty Web Application".
Step 3: Now click on Solution Explorer.
Step 4: Now right-click on the "ADD" > "New Item" > "Web Form" and add the name of the Web Form just like I did in WebForm1.aspx.
Step 5: After adding the WebForm1.aspx you will see the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
User Name:<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="Button2" runat="server" onclick="Button2_Click" text="Restore" />
</div>
</form>
</body>
</html>Now write the code as in the following:
// variable Declaration of x and y
public string x, y;
protected void Button1_Click(object sender, EventArgs e)
{
//TextBox1 and TextBox2 Value is Assigning on the variable x and y
x =
TextBox1.Text;
y =
TextBox2.Text;
//after clicking on Button TextBox value Will be Cleared
TextBox1.Text = string.Empty;
TextBox2.Text= string.Empty;
}
protected void Button2_Click(object sender, EventArgs e)
{
//value of variable x and y is assingning on TextBox1 and
Textbox2
TextBox1.Text = x;
TextBox2.Text = y;
It only happens because all the controls are classes and on the server all the Control Objects are created and then after the round trip the Page is returned to the client's browser in HTML format and the objects are destroyed at the server.
After the Submit button is clicked the value of user name and password is submited to the server. We cannot restore the value again because after the postback the instance of the control is destroyed and on clicking of the Restore Button the server takes a new request and the server cannot restore the value of the TextBox.
And now I am explaining the stored value in the View State and the remaining steps are the same as the previous
Code: Now write this code:
//
variable Declaration of x and y
public string x, y;
protected void Button1_Click(object sender, EventArgs e)
{
//TextBox1
and TextBox2 Value is Assigning on the variable x and y
ViewState["name"] =
TextBox1.Text;
ViewState["password"] =
TextBox2.Text;
//after
clicking on Button TextBox value Will be Cleared
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
protected void Button2_Click(object sender, EventArgs e)
{
//If
ViewState Value is not Null then Value of View State is Assign to TextBox
if (ViewState["name"] != null)
{
TextBox1.Text = ViewState["name"].ToString();
}
if (ViewState["password"] != null)
{
TextBox2.Text = ViewState["password"].ToString();
}
}
Output: Now the output is:
After clicking on the Submit Button the value of user name and password is submitted in View State and the View State stores the value of user name and password during post-back.
After click on the Restore Button we can get the value again. The Value must be retained during post-back and the values are stored into a base 64 encoded string and this information is then put into the View State Hidden Field.
Advantages of view state:
This are the main advantage of using View State:
• Easy to implement
• No server resources are required
• Enhanced security features, like it can be encoded and compressed.
Disadvantages of view state?
This are the main disadvantages of using View State:
• It can be performance overhead if we are going to store larger amount of data, because it is associated with page only.
• It’s stored in a hidden filed in hashed format (which I have discussed later) still it can be easily trapped.
• It does not have any support on mobile devices.
Enabling and Disabling View State
You can enable and disable View state for a single control as well as at page level also. To turnoff view state for a single control , set EnableViewState Property of that control to false. e.g.:
Hide Copy Code
TextBox1.EnableViewState =false;
To turnoff the view state of entire page, we need to set EnableViewState to false of Page Directive as shown bellow.
When We Should Use View State
1.When the data to be stored is small.
2.Try to avoid secure data.







No comments:
Post a Comment