Dynamically Recreated Child Controls not Retaining View State

E

Earl Teigrob

I am trying to write a control that will work as a place holder for
dynamically added controls (via an object array that is passed in by the
containing page) AND retains the view state of these child controls. Geting
the object and adding them as child controls is easy. I can also regenerate
the same child controls on postback, but can not get the child controls to
retain their viewstate. What am I doing wrong???
Another question is whether I sould be using INameingInterface?

Below is my custom control class

Thanks for your help!

Earl

Imports System.ComponentModel
Imports System.Web.UI

<Serializable(), DefaultProperty("Text"), ToolboxData("<{0}:TabMenu
runat=server></{0}:TabMenu>")> Public Class TabMenuA
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer

Private ControlsCreated As Boolean = False
Private _TabMenuItems() As TabMenuItem

<Bindable(True), Category("Data"), DefaultValue("")> Public Property
TabMenuItems() As TabMenuItem()
Get
Return _TabMenuItems
End Get
Set(ByVal Value As TabMenuItem())
_TabMenuItems = Value
End Set
End Property

'Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)

'End Sub

<Serializable()> _
Class TabMenuItem
Private _Row As Int32
Private _Order As Int32
Private _Value As String
<NonSerialized()> Private _RenderControl As
System.Web.UI.WebControls.WebControl
Private _ControlType As System.Type
Private _ControlId As String

Property Row() As Int32
Get
Return _Row
End Get

Set(ByVal Value As Int32)
_Row = Value
End Set
End Property

Property Order() As Int32
Get
Return _Order
End Get

Set(ByVal Value As Int32)
_Order = Value
End Set
End Property

Property Value() As String
Get
Return _Value
End Get

Set(ByVal Value As String)
_Value = Value
End Set
End Property

<System.Xml.Serialization.XmlIgnore()> _
Property RenderControl() As System.Web.UI.WebControls.WebControl
Get
If Not _RenderControl Is Nothing Then
Return _RenderControl
Else
_RenderControl =
System.Activator.CreateInstance(ControlType)
_RenderControl.ID = ControlId
Return _RenderControl
End If
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_RenderControl = Value
ControlType = Value.GetType
ControlId = Value.ID
End Set
End Property


Private Property ControlType() As System.Type
Get
Return _ControlType
End Get
Set(ByVal Value As System.Type)
_ControlType = Value
End Set
End Property

Private Property ControlId() As String
Get
Return _ControlId
End Get

Set(ByVal Value As String)
_ControlId = Value
End Set
End Property

End Class


Protected Overrides Sub CreateChildControls()
RenderTabControls(_TabMenuItems)
End Sub


Private Sub RenderTabControls(ByVal sbTabMenuItems As TabMenuItem())
If Not ControlsCreated Then
Me.Controls.Clear()
If Not sbTabMenuItems Is Nothing Then
If sbTabMenuItems.Length > 0 Then
For Each MenuItem As TabMenuItem In sbTabMenuItems
If Not MenuItem Is Nothing Then
Me.Controls.Add(MenuItem.RenderControl)
End If
Next
End If
End If
ControlsCreated = True
End If
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
Dim objViewStates() As Object
objViewStates = CType(savedState, Object())
_TabMenuItems = objViewStates(1)
RenderTabControls(_TabMenuItems)
MyBase.LoadViewState(objViewStates(0))
End Sub

Protected Overrides Function SaveViewState() As Object
Dim objViewStates(2) As Object
objViewStates(0) = MyBase.SaveViewState()
objViewStates(1) = _TabMenuItems
Return objViewStates
End Function
End Class
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top