One technique to store state on the client is view state. View state is used automatically by the Web server controls to make events work. The view state
contains the same state as the control when sent to the client. When the browser sends the
round trip to the server, the view state contains the original values, but the values of the controls that
are sent contain the new values. If there’s a difference, the corresponding event handlers are invoked.
The disadvantage of using view state is that data is always transferred from the server to the client,and vice versa, which increases network traffic. To reduce network traffic, view state can be turned off.
It is also possible to store custom data inside the view state. This can be done by using an indexer with the ViewState property of the Page class. You can define a name that is used to access the view state
value with the index argument:
The disadvantage of using view state is that data is always transferred from the server to the client,and vice versa, which increases network traffic. To reduce network traffic, view state can be turned off.
To do so for all controls within the page, set the EnableViewState property to false with the Page
directive:
<%@ Page
Language="C#"
AutoEventWireUp="true"
CodeFile="Default.aspx.cs"
Inherits="Default" EnableViewState="false" %>
The view state can also be configured on a control by setting the EnableViewState property of a control.Regardless of what the page configuration says, when the EnableViewState property is defined for the control, the control value is used. The value of the page configuration is used only for these controls when the view state is not configured.
It is also possible to store custom data inside the view state. This can be done by using an indexer with the ViewState property of the Page class. You can define a name that is used to access the view state
value with the index argument:
ViewState["mydata"]
= "my data";
You can read the previously stored view state as shown here:
string mydata = (string)ViewState["mydata"];
In the HTML code that is sent to the client, you can see the view
state of the complete page within a hidden field:
<input type="hidden"
name="
VIEWSTATE"
value="/wEPDwUKLTU4NzY5NTcwNw8WAh4HbXlzdGF0ZQUFbXl2YWwWAgIDD2QWAg
IFDw8WAh4EVGV4dAUFbXl2YWxkZGTCdCywUOcAW97aKpcjt1tzJ7ByUA==" />
Using hidden fields has the advantage that every browser can use
this feature, and the user cannot turn it off.
The view state is only remembered within a page. If the state
should be valid across different pages,then using cookies is an option for state on the client.
No comments:
Post a Comment