ARTICLE

Storing and Retrieving control data using View State in ASP.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 08, 2011
Here, we will see how to store data in more than one textbox control and retrieve that data using single view state.
 
Reader Level:

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:

storeimage1.gif

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.

storeimage2.gif

Figure2

Now click on the save Button.

storeimage1.gif

Figure3

Now click on the restore Button to retrieve data.

storeimage2.gif

Figure4

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor