Changing the order of child controls affects state management

Discussion in 'ASP .Net Web Controls' started by ErwinP, Feb 23, 2005.

  1. ErwinP

    ErwinP Guest

    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
     
    ErwinP, Feb 23, 2005
    #1
    1. Advertising

  2. ErwinP

    Wilco Bauwer Guest

    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.
     
    Wilco Bauwer, Feb 23, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,161
    David Schwartz
    Dec 9, 2003
  2. Replies:
    0
    Views:
    216
  3. Williams Williams
    Replies:
    2
    Views:
    134
    Stefano Crocco
    Sep 28, 2010
  4. fatted
    Replies:
    1
    Views:
    108
    John W. Krahn
    Nov 26, 2004
  5. 490
    Replies:
    1
    Views:
    96
    Mumia W.
    Mar 27, 2007
Loading...

Share This Page