Using the ViewState Property
In some cases, you do not need to maintain state across pages but rather only between calls to the same page. If you need to do this, you can use a StateBag object provided by ASP.NET.
Using the StateBag Object
You can use the ViewState property of the page. The ViewState property is a StateBag object. That is, the property is defined As StateBag. In the Web page shown in Figure 23.1, you input three values. You can submit them to the server and display some data in the Result label. At the same time, you create three variables in a "state bag" using the ViewState object.
Figure 23.1. Use this screen to test the StateBag object.
In the Click event procedure of the Create State button, write the following code to display the data and create the ViewState object:
Public Sub btnSubmit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnSubmit.Click ViewState("First") = txtFirst.Text ViewState("Last") = txtLast.Text ViewState("Password") = txtPassword.Text lblResult.Text = txtLast.Text & ", " & _ txtFirst.Text & " (" & txtPassword.Text & ")" lblStateResult.Text = ViewState.Count.ToString() End Sub
In this code sample, you use the ViewState object just like the Session object. Create a new variable just by supplying the name of the variable in parentheses and quotes and assigning a new value to it. Each of the values comes from the text boxes on the Web page.
While you are still on the same page, you can test to see whether the values in the state bag are really preserved during the roundtrip. You can click the Check State button to redisplay the values from the StateBag object (when you click the button, you're submitting the page to the server, forcing a roundtrip):
Public Sub btnStateCheck_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnStateCheck.Click lblResult.Text = ViewState("Last").ToString() & _ ", " & ViewState("First").ToString() lblStateResult.Text = ViewState.Count.ToString() End Sub
StateBag objects are only valid while the page is active. As soon as you navigate to another page, the StateBag object is discarded. If you really wish to give the ViewState property a test, you could click the Create State button and then restart the IIS Service. After restarting the IIS Service, click the Check State button and see whether all your values have been preserved. The ViewState property, with all the data, has been preserved in a hidden field in your form. This is a nice feature it means that restarting the Web server will not affect the data you are storing for that one page.
State Bag Issues
The StateBag object provides a nice mechanism for storing view state information: the ability to store state for an individual page across roundtrips to the server. However, like most techniques, it comes with its own set of issues:
The ViewState property is only valid for a single page. For multiple pages, you need to use another structure.
The more information you store in your StateBag object, the more data you have to exchange with your client's browser. This can cause performance problems across your whole site. Remember that the data must go back and forth with each post of the page back to the server.
Memory could become an issue. Because pages could grow quite large, with many controls, the ViewState information can take up a large amount of memory on the user's machine. This may cause the user's operating system to swap some memory to disk and slow down the user's machine even more.