Maintaining View State For Dynamically Created Controls

Discussion in 'ASP .Net' started by =?Utf-8?B?RWFybCBUZWlncm9i?=, Oct 7, 2004.

  1. I am building a custom control that I want to server as a container for child
    controls that can be dynamically added to this control. I can persist the
    child controls that are added to my custom control but can to get them to
    retain view state.

    Below is a striped down version of my control. It exposes one property of
    type System.Web.UI.Control that the containing page my set with a dynamically
    created control My custom control will then add it to its control tree and
    render it to the display. The information need to recreate the control is
    stored in a System.Web.UI.Triplet object. On postback, the child control is
    reinstanced and given its origonal id and added to the Custom Controls
    control tree and rendered to the display.

    The only problem is that the ViewState for the child control is not being
    retained.

    Has anyone solved this puzzle???

    Thanks for your help

    Earl

    CUSTOM WEB SERVER CONTROL

    Imports System.ComponentModel
    Imports System.Web.UI

    <ToolboxData("<{0}:TabMenuA runat=server></{0}:TabMenuA>")> Public Class
    ContainerControl
    Inherits System.Web.UI.WebControls.WebControl

    #Region "Declarations"

    Private _ChildControlToRender As System.Web.UI.Control

    #End Region

    #Region "Properties"

    <Bindable(True), Category("Data"), DefaultValue("")> Public Property
    ChildControlToRender() As System.Web.UI.Control
    Get
    Return _ChildControlToRender
    End Get
    Set(ByVal Value As System.Web.UI.Control)
    _ChildControlToRender = Value
    End Set
    End Property

    #End Region

    #Region "Methods"

    Protected Overrides Sub CreateChildControls()
    If Not Page.IsPostBack Then
    Controls.Add(_ChildControlToRender)
    End If

    End Sub


    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    Dim objViewStates() As Object
    objViewStates = CType(savedState, Object())
    Dim Trip As System.Web.UI.Triplet
    Trip = objViewStates(1)
    _ChildControlToRender = Activator.CreateInstance(CType(Trip.Second,
    System.Type))
    _ChildControlToRender.ID = Trip.First
    Me.Controls.Add(_ChildControlToRender)
    MyBase.LoadViewState(objViewStates(0))
    End Sub

    Protected Overrides Function SaveViewState() As Object
    Dim objViewStates(1) As Object
    Dim Trip As New System.Web.UI.Triplet
    Trip.First = _ChildControlToRender.ID
    Trip.Second = _ChildControlToRender.GetType
    Trip.Third = Me.ID
    objViewStates(1) = Trip
    objViewStates(0) = MyBase.SaveViewState()
    Return objViewStates
    End Function

    #End Region
    End Class

    CONTAINING PAGE CODE

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
    Dim myButton As Button = New Button
    myButton.Text = "Hello1"
    myButton.ID = "ButtonA"
    myButton.EnableViewState = True
    ContainerControl1.ChildControlToRender = myButton
    End If

    End Sub
     
    =?Utf-8?B?RWFybCBUZWlncm9i?=, Oct 7, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGV0ZSBNb3Nz?=

    Maintaining state with a dynamically created TemplateColumn

    =?Utf-8?B?UGV0ZSBNb3Nz?=, Apr 5, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    926
    =?Utf-8?B?UGV0ZSBNb3Nz?=
    Apr 8, 2006
  2. Replies:
    0
    Views:
    427
  3. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
  4. Earl Teigrob

    Maintaining View State For Dynamically Created Controls

    Earl Teigrob, Oct 7, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    153
    Earl Teigrob
    Oct 7, 2004
  5. Replies:
    3
    Views:
    194
Loading...

Share This Page