E
ErwinP
Hi,
I have developed a composite control which adds child controls from a
definition in an XML document. The order in which these child controls
are added to the composite control may vary across postbacks. This
change in ordering of child controls does not seem to make any
difference... EXCEPT when I programmatically change the visibility of
my composite control to 'Invisible' and then back to 'Visible'. After
that state management is messed up and restores values into the wrong
child controls.
Does anyone have any idea how changing the order of child controls
combined with changing the visibility of the entire composite control
can have such an impact on state management? Any help would be greatly
appreciated!
Kind regards,
Erwin
P.S.: to illustrate the problem, I've developed a simple composite
control which reproduces the behavior I've encountered. The control
creates three textboxes and randomly adds them to the Controls
collection. The order on screen however is always the same. Put the
control on a webform and add two buttons: one for making the control
invisible and one for making it visible. When finished, start the form
and do the following:
- fill all three textboxes with a different value
- press the 'Make visible' button several times (the order of controls
changes as the number in front of the first textbox indicates), but
the values stay in the same textbox
- now press the 'Make invisible' and then 'Make visible' button
repeatedly and notice how the textbox values jump around
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<ToolboxData("<{0}:ControlOrdering
runat=server></{0}:ControlOrdering>")> _
Public Class ControlOrdering
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
Protected Overrides Sub RenderContents(ByVal output As
System.Web.UI.HtmlTextWriter)
output.Write(CType(_remainder, String))
_t1.RenderControl(output)
_t2.RenderControl(output)
_t3.RenderControl(output)
End Sub
Protected Overrides Sub CreateChildControls()
' create the child controls
_t1 = New TextBox
_t1.ID = "t1"
_t2 = New TextBox
_t2.ID = "t2"
_t3 = New TextBox
_t3.ID = "t3"
' add the controls in random order
Dim rnd As New System.Random
Dim newRandomNumber As Integer = rnd.Next
_remainder = newRandomNumber Mod 6
Select Case _remainder
Case 0
Controls.Add(_t1)
Controls.Add(_t2)
Controls.Add(_t3)
Case 1
Controls.Add(_t1)
Controls.Add(_t3)
Controls.Add(_t2)
Case 2
Controls.Add(_t2)
Controls.Add(_t1)
Controls.Add(_t3)
Case 3
Controls.Add(_t2)
Controls.Add(_t3)
Controls.Add(_t1)
Case 4
Controls.Add(_t3)
Controls.Add(_t1)
Controls.Add(_t2)
Case 5
Controls.Add(_t3)
Controls.Add(_t2)
Controls.Add(_t1)
End Select
End Sub
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Private _t1, _t2, _t3 As TextBox
Private _remainder As Integer
End Class
I have developed a composite control which adds child controls from a
definition in an XML document. The order in which these child controls
are added to the composite control may vary across postbacks. This
change in ordering of child controls does not seem to make any
difference... EXCEPT when I programmatically change the visibility of
my composite control to 'Invisible' and then back to 'Visible'. After
that state management is messed up and restores values into the wrong
child controls.
Does anyone have any idea how changing the order of child controls
combined with changing the visibility of the entire composite control
can have such an impact on state management? Any help would be greatly
appreciated!
Kind regards,
Erwin
P.S.: to illustrate the problem, I've developed a simple composite
control which reproduces the behavior I've encountered. The control
creates three textboxes and randomly adds them to the Controls
collection. The order on screen however is always the same. Put the
control on a webform and add two buttons: one for making the control
invisible and one for making it visible. When finished, start the form
and do the following:
- fill all three textboxes with a different value
- press the 'Make visible' button several times (the order of controls
changes as the number in front of the first textbox indicates), but
the values stay in the same textbox
- now press the 'Make invisible' and then 'Make visible' button
repeatedly and notice how the textbox values jump around
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<ToolboxData("<{0}:ControlOrdering
runat=server></{0}:ControlOrdering>")> _
Public Class ControlOrdering
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
Protected Overrides Sub RenderContents(ByVal output As
System.Web.UI.HtmlTextWriter)
output.Write(CType(_remainder, String))
_t1.RenderControl(output)
_t2.RenderControl(output)
_t3.RenderControl(output)
End Sub
Protected Overrides Sub CreateChildControls()
' create the child controls
_t1 = New TextBox
_t1.ID = "t1"
_t2 = New TextBox
_t2.ID = "t2"
_t3 = New TextBox
_t3.ID = "t3"
' add the controls in random order
Dim rnd As New System.Random
Dim newRandomNumber As Integer = rnd.Next
_remainder = newRandomNumber Mod 6
Select Case _remainder
Case 0
Controls.Add(_t1)
Controls.Add(_t2)
Controls.Add(_t3)
Case 1
Controls.Add(_t1)
Controls.Add(_t3)
Controls.Add(_t2)
Case 2
Controls.Add(_t2)
Controls.Add(_t1)
Controls.Add(_t3)
Case 3
Controls.Add(_t2)
Controls.Add(_t3)
Controls.Add(_t1)
Case 4
Controls.Add(_t3)
Controls.Add(_t1)
Controls.Add(_t2)
Case 5
Controls.Add(_t3)
Controls.Add(_t2)
Controls.Add(_t1)
End Select
End Sub
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Private _t1, _t2, _t3 As TextBox
Private _remainder As Integer
End Class