View State:
View State which is used to maintain the State at a page level.
i.e the the information is being stored for a specific page and until that specific page is active (i.e. the page which we are currently viewing). if we are redirected to some other page, the information stored in the View State will be lost.
It uses "Dictionary Object" to store data, here the information is stored in key and value pair. It stores that information in a Hidden field on the page itself in a hashed format.
It can store a string value upto specific length. If the length is exceeded then the excess information will be stored into another hidden field.
It maintains the state of controls during page postback and if we save any control values or anything in the viewstate than we can access that values throughout the page whenever it is required.
viewstate values are by default sent to the client browser and it returned to the server in the form of a hidden input control on your page
Basic Example of View State:
In, this example i am going to save the view state values, and retrieving it on PageLoad
.cs page:
protected void btnSave_Click(object sender, EventArgs e)
{
ViewState["FirstName"] = Convert.ToString(txtFirstName.Text);
ViewState["LastName"] = Convert.ToString(txtLastName.Text);
ViewState["Salary"] = Convert.ToString(txtSalary.Text);
txtFirstName.Text = "";
txtLastName.Text = "";
txtSalary.Text = "";
}
protected void btnRead_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["FirstName"])))
{
txtFirstName.Text = Convert.ToString(ViewState["FirstName"]);
}
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["LastName"])))
{
txtLastName.Text = Convert.ToString(ViewState["LastName"]);
}
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["Salary"])))
{
txtSalary.Text = Convert.ToString(ViewState["Salary"]);
}
}
.In this example, i am saving the values in ViewState and Reteriving the saved values from viewstate , when we click on Read button.
When Click on Read Button. again the values are populated.
we can see below I am retrieving the value from the View State,
View State Information is stored in a Hashed Format
In html view source ,we can see this
View Source we can see this as,In html view source ,we can see this
View State can be applied at the following places.
Page Level.
If we dont want the page to use viewstate,then we can disable view state at page level.
we can disable or enable viewstate by using EnableViewState
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableViewState="false"%>
Example:
In the below example i am saving the values in ViewState, and when i am trying to Read those values from Viewstate , we can see the ViewState Values are null.
Output:
View State which is used to maintain the State at a page level.
i.e the the information is being stored for a specific page and until that specific page is active (i.e. the page which we are currently viewing). if we are redirected to some other page, the information stored in the View State will be lost.
It uses "Dictionary Object" to store data, here the information is stored in key and value pair. It stores that information in a Hidden field on the page itself in a hashed format.
It can store a string value upto specific length. If the length is exceeded then the excess information will be stored into another hidden field.
It maintains the state of controls during page postback and if we save any control values or anything in the viewstate than we can access that values throughout the page whenever it is required.
viewstate values are by default sent to the client browser and it returned to the server in the form of a hidden input control on your page
Basic Example of View State:
In, this example i am going to save the view state values, and retrieving it on PageLoad
.cs page:
protected void btnSave_Click(object sender, EventArgs e)
{
ViewState["FirstName"] = Convert.ToString(txtFirstName.Text);
ViewState["LastName"] = Convert.ToString(txtLastName.Text);
ViewState["Salary"] = Convert.ToString(txtSalary.Text);
txtFirstName.Text = "";
txtLastName.Text = "";
txtSalary.Text = "";
}
protected void btnRead_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["FirstName"])))
{
txtFirstName.Text = Convert.ToString(ViewState["FirstName"]);
}
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["LastName"])))
{
txtLastName.Text = Convert.ToString(ViewState["LastName"]);
}
if (!string.IsNullOrEmpty(Convert.ToString(ViewState["Salary"])))
{
txtSalary.Text = Convert.ToString(ViewState["Salary"]);
}
}
.In this example, i am saving the values in ViewState and Reteriving the saved values from viewstate , when we click on Read button.
When Click on Read Button. again the values are populated.
we can see below I am retrieving the value from the View State,
View State Information is stored in a Hashed Format
In html view source ,we can see this
View Source we can see this as,In html view source ,we can see this
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="vkITvu/CexBloorOExhQydean8rSPGP8ZyiqbLqc71XNJYBLyVpWe1Fow2yFyEN43Bwh8RNard1nTm7ZZkCx99VvcjCcPGn7Y0DPvMO3WgzFbk3Xduw3HTi/aQZF6UYkd+m/nYsjvNnzVWd4b8USvmkNzAgz8ZxcCWL8v8LAJLcwiGo74HqrKcsQwKcmKBzfIfjovSysuFpRQEDa+Syk1Q==" />
</div>
-----------------------------------------------------------------------------------------View State can be applied at the following places.
- Application Level
- Page Level
- Control Level
Page Level.
If we dont want the page to use viewstate,then we can disable view state at page level.
we can disable or enable viewstate by using EnableViewState
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableViewState="false"%>
Example:
In the below example i am saving the values in ViewState, and when i am trying to Read those values from Viewstate , we can see the ViewState Values are null.
Output:
At Control Level
Application Level
In web.config
If View State set to off. Textbox values still Existings
They are few controls which retain there values even after disabling the ViewState?
The controls which implements IPostBackEventHandler (Textbox, Checkbox) will retain the state even though if disable the viewstate. That is during the Load Postback Data phase, that controls will get state information from Posted back form.
Where as controls like label will not implement IPostBackEventHandler will not get any state information from posted back data and hence they depend on viewstate to maintain the state .
To make view state secure?
There are two ways :
"EnableViewStateMAC=true" (MAC Stands for "Message Authentication Code")
ViewStateEncryptionMode="Always" , which will encrypt the view state data.
ViewStateEncryptionMode.Auto
It will Encrypt the ViewState.
ViewStateEncryptionMode.Never
It will not encrypt the ViewState
ViewStateEncryptionMode.Always
ViewState is always encrypted
The following are list of controls which dont maintain state if we disable viewstate also. That is because they implement IPostBackDataHandler
TextBox
CheckBox
CheckBoxList
DropDownList
ImageButton
ListBox
RadioButtonList
HtmlInputCheckBox
HtmlInputFile
HtmlInputHidden
HtmlInputImage
HtmlInputRadioButton
HtmlInputText
HtmlSelect
HtmlTextArea
No comments:
Post a Comment