Changing the order of child controls affects state management

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
 
W

Wilco Bauwer

You can work around this by not changing the order of the controls in
the control collection, but by rendering them in a different order
instead. I wrote a custom control (a webpartmanager) and an article
about this control at
http://wilcoding.xs4all.nl/Wilco/View.aspx?NewsID=144. It explains how
you can solve the problem where you want to be able to postback and let
a different control hierarchy appear (like moved controls).

HTH.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top