Click here to Skip to main content
15,893,814 members
Articles / Web Development / ASP.NET
Article

Save and Get values of Dynamically created controls from ViewState

Rate me:
Please Sign up or sign in to vote.
1.04/5 (15 votes)
13 Nov 20031 min read 79.7K   20   5
This articles describes you a simple method to save and get values of dynamically created controls in a Web Page from the ViewState Object

Introduction

Firstly thanks to Paul Wilson whose articles have enlightened me about the ViewState Object.
This listing (an extract of Paul's article) explains events that take place in a client requested aspx page.

Listing1

There are many programmers like me who would like to create dynamic controls
in a page on/after the Page_load event and still use the ViewState to store and
retrieve information about the controls value. Though the saving of information
is easy the retrieval part is not easy. By defining a functions in the PageClass
we can store and get information of all the dynamic controls that need to save
information in the ViewState object.

Using the code

Step 1: Create a page class

<P>Public Class BasePage</P><P>      Inherits System.Web.UI.Page</P><P> End Class</P>

Step 2: Declare the following functions in this class 

<P>Public Function SaveCtrlViewState(ByVal ID As String, ByVal value As Object)</P><P>    Me.ViewState(ID) = value</P><P>End Function </P><P>  </P><P>Public Function GetCtrlViewState(ByVal ID As String) As Object</P><P>     Return Me.ViewState(ID)</P><P>End Function</P>

Step 3: Create a WebControl Class

<P>Public Class DynTextBox</P><P>    Inherits System.Web.UI.WebControls.TextBox</P><P>    Private _page as BasePage </P><P> </P><P>    Protected Overrides Sub OnInit(ByVal E As System.EventArgs)</P><P>    MyBase.OnInit(E)  </P><P>      _page = CType(Me.Page, BasePage)  </P><P>    End Sub</P><P>    Public Sub DoSomething()</P><P>       _page.SaveCtrlViewState(Me.ID, value)</P><P>    End Sub</P><P>    Public Sub DoSomethingMore()</P><P>       Dim oValue As Object = _page.GetCtrlViewState(Me.ID)</P><P>    End Sub</P><P> </P><P>End Class</P>

Step 4: Create a page that is derived from BasePage

Step 5: Create an instance of DynTextBox control in any event/Function (before Render)

in your Page

Step 6: Save the ViewState of the control by calling DoSomething function.

Step 7: Postback your page (have a Submit button)

Step 8: Create your control and call the function <DoSomethingMore> to get back the

control's ViewState

You finally have the flexibility to save your dynamic controls ViewState and get it back

without worrying much about the events that happen in your page.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Sunil Menon is a FrameWork Team Leader in ITB India Pvt. Ltd. He has been working in VC++ and Centura before getting hooked to .Net. After creating some applications in the Compact Framework, his skills are now being exploited in the ASP.net world. He loves his work and loves solving problems and designing workflows.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Pranay Rana5-Aug-10 2:10
professionalPranay Rana5-Aug-10 2:10 
GeneralI also need the same thing.. Pin
VarunPrakash18-Aug-09 18:42
VarunPrakash18-Aug-09 18:42 
Generalyou example is no good Pin
mattknapp10-Feb-04 8:51
mattknapp10-Feb-04 8:51 
GeneralSample Pin
sides_dale14-Nov-03 13:44
sides_dale14-Nov-03 13:44 
GeneralRe: Sample Pin
George L. Jackson20-Nov-03 8:27
George L. Jackson20-Nov-03 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.