As discussed in the In Depth section of this chapter, all the data in a Web form isn't necessarily preserved between round trips to the server. By default, Visual Basic stores the data in Web server controls and HTML server controls in an HTML hidden field in the Web page (that is, HTML <input> elements with the type attribute set to "hidden"), so that data is preserved between round trips to the server. However, what about the variables in your code? That data isn't stored automatically, so after the page goes to the server and comes back, the values in your variables will be reset to their default values (usually 0).
However, you can make sure the data in your variables is stored over server round trips, and you can do so in several different ways. For example, say you have a variable named value1 that you increment every time the user clicks a button, displaying the new value. If you didn't save the value in value1 between server round trips, it would be set to 0 after every such trip, and if you incremented it and displayed the resulting value, the user would always see a value of 1, no matter how many times they clicked the button. How do you fix this problem?
I'll take a look at a couple of ways of saving the value in value1 during server round trips in the RoundTrip example from the CD-ROM, which you see in Figure 14.20. When you click any of the three buttons in that example, the code increments one of three counters, whose value is preserved between trips to the server; you'll see "New value: 1", then "New value: 2", then "New value: 3", and so on as you keep clicking a button.
The first technique I'll take a look at, illustrated by the first button in this example, is a server-side technique, where you store data on the server, using the ASP .NET Session object. Each time someone works with your Web application, a new session is created and given a long randomly generated ID value. You can store and retrieve values using the Session object in Visual Basic code; here's how that looks when the user clicks the first button (caption: "Use Session"):
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim value1 As Integer = Session("value1") value1 += 1 TextBox1.Text = "New value: " & value1 Session("value1") = value1 End Sub
Besides saving values on the server, you also can save them in the Web page itself. By default, Visual Basic stores data in an HTML hidden control in a Web page. The data in that control corresponds to a StateBag object, and you can reach that object yourself using a control's ViewState property. Because Web forms are based on the Control class, you can use the Web form's ViewState property to store and retrieve your data across server round trips. Here's how that works when the user clicks the second button in the RoundTrips example (caption: "Use ViewState"):
You also can create your own HTML hidden field in a Web form and use that to store data in. You can add a hidden field to a Web form by clicking the HTML tab in the toolbox and double-clicking the Hidden tool. You need to make this field into a server control so its data will be preserved across server round trips, so right-click it and select the Run As Server Control item. Finally, give it an ID value, using the (id) property in the Properties window, of Hidden1. Now you can access the data in this hidden control as Hidden1.Value, so here's how I use this control with the third button in the example (caption: "Use Hidden controls"):
Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click Hidden1.Value += 1 TextBox1.Text = "New value: " & Hidden1.Value End Sub
Found on page: