Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Changing the order of child controls affects state management

Reply
Thread Tools

Changing the order of child controls affects state management

 
 
ErwinP
Guest
Posts: n/a
 
      02-23-2005
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

 
Reply With Quote
 
 
 
 
Wilco Bauwer
Guest
Posts: n/a
 
      02-23-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3x3 Array of int, changing one value affects others Williams Williams Ruby 2 09-28-2010 09:05 AM
Changing one array affects the other 490 Perl Misc 1 03-27-2007 12:23 PM
HELP! Child controls of child controls not visible to web app lisa@starways.net ASP .Net Building Controls 0 05-05-2005 09:14 PM
changing local variable affects array values? fatted Perl Misc 1 11-26-2004 01:55 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments