Introduction to ASP.NET ViewState
ViewState is normally defined as maintaining proper control in a page, suppose considering one web application, the request came from the browser, then application process that request and display in the screen. But on the screen on that specific view or page control all the attributes are actually binding with proper control, those attribute easily can be useable anywhere from the screen. This is one of the states of the application where attribute data are available only for that page, and when we refresh the page it will be again out of control, as like it holding the data only for the default ViewState.
Syntax of ASP.NET ViewState
ASP.NET ViewState can have multiple characteristics, and one of the keys or default feature for any of the web application. Normally any of the web applications considering as stateless. That’s means whenever any new request came to one web application, it created one page, but if we do a round trip this entire control or specific page immediately lost and coming to the response as a new page with new control.
The state control of ASP.NET web application, can be considered as two major states:
1. State Management in Client-side
- View State: Control the attributes just within the same view or page, it is the default state for ASP.NET web application.
- Control State: Client-side controller, write the logic for holding data for full flow controller of multiple views.
- Hidden Fields State: Keeping some fields as hidden and transferring attribute value to the next page.
- Cookies State: Set the specific values in cookies, which are holding that specific data in the browser until it is not clear.
- Query String: Passing data from controller to page by passing the query string, data available in the view page easily.
2. State Management in Server-side
- Application context state: Data can be hold in the application context, this data can be available for the entire application lifecycle, or till the next server restart.
- Session State: Data can be stored as a session, this data will be available for the entire session lifecycle.
- Profile Properties: Store some attributes data in a properties file, and passing that value in the application context, so that it will be available in view page for use.
ViewState is the default state for any ASP.NET web application. Suppose like below, we have created one HTML page, and writing that backend logic of that page in ASPX class. Now when that HTML page runs through this web application, then the server creates the object of that class, executing the logic, returning the result, and full control to the view or HTML page, then destroy the object or reference of those classes. Now the HTML page has full control over the response data for displaying on the screen.
<title>Sample Web Page</title>
Name: <asp:textbox id="name" runat="server"></asp:textbox>
Password: <asp:textbox id="pass" runat="server"></asp:textbox>
<asp:button id="submit_button" runat="server" onclick=" submit_button _Click" text="Submit" />
<asp:button id="Resturn" runat="server" onclick="Return_Click" text="Return" />
public string text1, text2;
protected void submit_button _Click(object sender, EventArgs e)
text1 = name.Text;
text2 = pass.Text;
name.Text = pass.Text = string.Empty;
protected void Return_Click(object sender, EventArgs e)
name.Text = text1;
pass.Text = text2;
Example of ASP.NET ViewState
Given below is the example of ASP.NET ViewState:
There have verities of data objects stored in view state in case of ASP.NET web application.
- String value
- Boolean type value
- Array type of object
- Array list kind of object
- Hash table kind of collection
- Any kind of converters which can be considered as custom type
Create a new project in visual studio for view set project in the ASP.NET.
Choose ASP.NET framework 4 for complete the application.
Include HTML code in the application for creating the view in presentation.
Add specific item and map it with a specific attribute within the HTML page.
Choose a specific template for the further process of designing.
Choosing design for WebForm1.aspx from the toolbox. Normally two text boxes came where 4 buttons design option came for choose.
Right-click on each button – click on properties – rename button names as given design.
Write the WebForm1.ASPX code for handling request in the web application, so that request can be a push to web application view and pushing data can behold as a view state.
Write the corresponding method for handling every button a specific event, based on that same action that can be executed on the view page.
Design the corresponding view page for the final presentation of the initial page.
Click on start debugging button for further configuration of the event action.
Screen output—Screen came with user name, password view, with multiple buttons like submit, restore, view enabled submit and view enables restore.
Given some username and password, then click submit, it resets the entire page for further entry.
Now in this scenario, if we click on restore button, it actually not given anything, because view state will not hold any data after page refresh.
Now after given user name and password and click on View Enables Submit button, it will give require result.
Clicking View Enabled Submit button textboxes are cleared.
Click on View Enabled Re-store last stored data from view state is displayed as below. Basically, passing username and password have been stored as a view state in the page life. So when restore has been clicked passing data hold in view state coming into the page as user name and password.
One more critical functionality of view state is it can be enabled or disabled for the specific text box on the web page.
It is very easy to do with two approaches:
1. One is, it can be disabled for one specific text field by below command.
TextBox11.EnableViewState = false;
2. The second approach is to disable the view state for the entire page.
<%Page Language=”C#” EnableViewState=”false”; %>
ASP.NET view state is really a wonderful feature coming with a web application by default. It has verities advantages and disadvantages as well, but still the most popular features for the ASP.NET. Implementation of view state in any web application is very easy and there has no requirement of server-specific resources to use view state development. For any time, page loading ensuring of load view state structure in the corresponding page. Whereas in terms of web security, application performance view state approach sometimes explicitly disables by the developer.
This is a guide to ASP.NET ViewState. Here we discuss the introduction to ASP.NET ViewState along with example respectively. You may also have a look at the following articles to learn more –