Here, we will see how to store data in more
than one textbox control and retrieve that data using single view state. We drag
and drop some textbox on the form with save and Restore Button control on the
form. When we enter some data in the textbox and click on the save Button. All
textbox will be empty and click on the restore Button to retrieve data back in text boxes.
Step-1
we will create a simple input form, like below:

Figure1
Step 2:
Now double click on the save Button to save all
the control's data into the view state.
Protected Sub cmdSave_Click(ByVal
sender As Object,
ByVal e As EventArgs)
' Save the current text.
SaveText(Page.Controls, True)
End Sub
Private Sub SaveText(ByVal
controls As
ControlCollection, ByVal saveNested
As Boolean)
For Each control
As Control In controls
If TypeOf
control Is TextBox Then
' Store the text using the unique
control ID.
ViewState(control.ID) = DirectCast(control,
TextBox).Text
'Clear text from controls
DirectCast(control,
TextBox).Text = ""
End If
If (control.Controls
IsNot Nothing) AndAlso saveNested Then
SaveText(control.Controls, True)
End If
Next
End Sub
Step-3
Now double click on the restore Button to
retrieve all data from view state.
Protected Sub cmdRestore_Click(ByVal
sender As Object,
ByVal e As EventArgs)
RestoreAllText(Page.Controls, True)
End Sub
Private Sub
RestoreAllText(ByVal controls
As ControlCollection,
ByVal saveNested As Boolean)
For Each control
As Control In controls
If TypeOf
control Is TextBox Then
If ViewState(control.ID)
IsNot Nothing Then
DirectCast(control,
TextBox).Text = DirectCast(ViewState(control.ID),
String)
End If
End If
If (control.Controls
IsNot Nothing) AndAlso saveNested
Then
RestoreAllText(control.Controls, True)
End If
Next
End Sub
Now run the application enter the data in
textboxes.

Figure2
Now click on the save Button.

Figure3
Now click on the restore Button to retrieve
data.

Figure4