How to use ViewState in VB.NET

You as software developer can use code to add information directly to the view state collection of the containing page and recover it later after the page is posted back. The type of information you can store includes not only the simple data types, but your custom objects too.   The view state collection is provided by ViewState property of the page. To add and remove items in this class, you should use a dictionary based syntax, where every item has a unique string name:

ViewState(“ZIPCode”) = 123456

When you need to restore (retrieve) the value, you should use the key name. You need to cast the retrieved value to the appropriate data type, because the ViewState collection stores all items as generic objects, which allows it to handle many different data types:

Dim zipCode as Integer
zipCode = CInt(ViewState(“ZIPCode”))