
260
|
Chapter 12: The Page Class
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
LocalColor = System.Drawing.Color.Black
End If
Message.ForeColor = LocalColor
Message.Text = "Label color is " & LocalColor.Name
ViewState("LabelColor") = LocalColor
Else
Message.ForeColor = System.Drawing.Color.Black
LocalColor = Message.ForeColor
Message.Text = "Label color is " & LocalColor.Name
ViewState("LabelColor") = LocalColor
End If
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:button id="button"
text="Click to change label color"
runat="server"/>
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
Notes
ViewState, in addition to managing state for server controls automatically, is a
convenient place for ambient page state that needs to be maintained from request
to request. In addition to storing primitive data types such as integers and strings,
the
StateBag class can be used to store objects, as long as those objects support
serialization, as does the Color structure in the example. When you store an
object that supports serialization in ViewState, the object’s state is automatically
serialized into a form that can be stored in ViewState and deserialized into an
object instance when you reference the object again.
Because ViewState does not store type information with the object, you must cast
the object retrieved ...