E
Earl Teigrob
I can not get the child controls of my custom control to retain view state
To use this control
initial page load
1. the containing page dynamically creates a control (button, in this case)
and assigns it to the property "ControlToCreate".
2. This property saves the type and id of the control in view state.
3. The CreateChildControls Method Adds the control to its control collection
4. The Button is Rendered to the Display
on Postback
1. The Containing page code to add the child control to the custom control
is not run because it is in a Not IsPostback test.
2. The CreateChildControls Method of the Custom Controls Calls the Property
that holds the child control.;
3. Becuase the child control field _ControlToRender is nothing, The Control
is recreated with the Activator.CreateInstance method from the Type and the
ID that where saved in ViewState.
4. The control is added as a child control of the custom control
5. The "Regenerated" control is rendered to the display BUT WITHOUT
VIEWSTATE!!! All control viewstate settings are lost including the control
text property
The empty button is rendered to the display and in the trace, I can see the
button in the control tree.
Has someone solved this mystery???
Thanks
Earl
CUSTOM CONTROL
Imports System.ComponentModel
Imports System.Web.UI
<Serializable(), ToolboxData("<{0}:TabMenuA runat=server></{0}:TabMenuA>")>
Public Class ContainerControl
Inherits System.Web.UI.WebControls.WebControl
#Region "Declarations"
Private _ChildControlToRender As System.Web.UI.WebControls.WebControl
#End Region
#Region "Properties"
<Bindable(True), Category("Data"), DefaultValue("")> Public Property
ChildControlToRender() As System.Web.UI.WebControls.WebControl
Get
If Not _ChildControlToRender Is Nothing Then
Return _ChildControlToRender
Else
_ChildControlToRender =
Activator.CreateInstance(CType(ViewState("Type"), System.Type))
_ChildControlToRender.ID = ViewState("ID")
Return _ChildControlToRender
End If
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_ChildControlToRender = Value
ViewState("Type") = Value.GetType
ViewState("ID") = Value.ID
End Set
End Property
#End Region
#Region "Methods"
Protected Overrides Sub CreateChildControls()
Controls.Add(ChildControlToRender)
End Sub
#End Region
End Class
CONTAINING PAGE CODE BEHIND
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"
ContainerControl1.ChildControlToRender = myButton
End If
End Sub
To use this control
initial page load
1. the containing page dynamically creates a control (button, in this case)
and assigns it to the property "ControlToCreate".
2. This property saves the type and id of the control in view state.
3. The CreateChildControls Method Adds the control to its control collection
4. The Button is Rendered to the Display
on Postback
1. The Containing page code to add the child control to the custom control
is not run because it is in a Not IsPostback test.
2. The CreateChildControls Method of the Custom Controls Calls the Property
that holds the child control.;
3. Becuase the child control field _ControlToRender is nothing, The Control
is recreated with the Activator.CreateInstance method from the Type and the
ID that where saved in ViewState.
4. The control is added as a child control of the custom control
5. The "Regenerated" control is rendered to the display BUT WITHOUT
VIEWSTATE!!! All control viewstate settings are lost including the control
text property
The empty button is rendered to the display and in the trace, I can see the
button in the control tree.
Has someone solved this mystery???
Thanks
Earl
CUSTOM CONTROL
Imports System.ComponentModel
Imports System.Web.UI
<Serializable(), ToolboxData("<{0}:TabMenuA runat=server></{0}:TabMenuA>")>
Public Class ContainerControl
Inherits System.Web.UI.WebControls.WebControl
#Region "Declarations"
Private _ChildControlToRender As System.Web.UI.WebControls.WebControl
#End Region
#Region "Properties"
<Bindable(True), Category("Data"), DefaultValue("")> Public Property
ChildControlToRender() As System.Web.UI.WebControls.WebControl
Get
If Not _ChildControlToRender Is Nothing Then
Return _ChildControlToRender
Else
_ChildControlToRender =
Activator.CreateInstance(CType(ViewState("Type"), System.Type))
_ChildControlToRender.ID = ViewState("ID")
Return _ChildControlToRender
End If
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_ChildControlToRender = Value
ViewState("Type") = Value.GetType
ViewState("ID") = Value.ID
End Set
End Property
#End Region
#Region "Methods"
Protected Overrides Sub CreateChildControls()
Controls.Add(ChildControlToRender)
End Sub
#End Region
End Class
CONTAINING PAGE CODE BEHIND
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"
ContainerControl1.ChildControlToRender = myButton
End If
End Sub