Click here to Skip to main content
Click here to Skip to main content

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

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

Share

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 ASP.net world. He loves his work and loves solving problems and designing workflows.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberPranay Rana5-Aug-10 3:10 
GeneralI also need the same thing.. PinmemberVarunPrakash18-Aug-09 19:42 
Generalyou example is no good Pinmembermattknapp10-Feb-04 9:51 
GeneralSample Pinmembersides_dale14-Nov-03 14:44 
GeneralRe: Sample PinmemberGeorge L. Jackson20-Nov-03 9:27 

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

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

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