Click here to Skip to main content
12,998,759 members (74,965 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 13 Nov 2003

Save and Get values of Dynamically created controls from ViewState

, 13 Nov 2003
Rate this:
Please Sign up or sign in to vote.
This articles describes you a simple method to save and get values of dynamically created controls in a Web Page from the ViewState Object


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.


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.


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


About the Author

Madampath Sunil Menon
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 world. He loves his work and loves solving problems and designing workflows.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Pranay Rana5-Aug-10 2:10
memberPranay Rana5-Aug-10 2:10 
GeneralI also need the same thing.. Pin
VarunPrakash18-Aug-09 18:42
memberVarunPrakash18-Aug-09 18:42 
Generalyou example is no good Pin
mattknapp10-Feb-04 8:51
membermattknapp10-Feb-04 8:51 
GeneralSample Pin
sides_dale14-Nov-03 13:44
membersides_dale14-Nov-03 13:44 
GeneralRe: Sample Pin
George L. Jackson20-Nov-03 8:27
memberGeorge 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 14 Nov 2003
Article Copyright 2003 by Madampath Sunil Menon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid