RE: Updating control collection from nested property

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bruce Parker (bparkerhsd_at_nospam.nospam)
Date: 09/16/04


Date: Thu, 16 Sep 2004 06:49:02 -0700

Please take note of my example. I have a class that inherits from WebControl:

Public Class PageFooter : Inherits WebControl : Implements INamingContainer

Inside this class is the call to CreateChildControls:

Protected Overrides Sub CreateChildControls()
  'this is where I add to the controls collection
End Sub

Also inside this class is a property:

Private oLeftButton1 As New LeftButton1Properties

Property LeftButton1() As LeftButton1Properties
        Get
            Return oLeftButton1
        End Get
        Set(ByVal Value As LeftButton1Properties)
            oLeftButton1 = Value
        End Set
    End Property

LeftButton1Properties is another class

Public Class LeftButton1Properties

Inside the LeftButton1Properties class is a property

Property [Enabled]() As Boolean
        Get
            Return mbEnabled
        End Get

        Set(ByVal Value As Boolean)
            mbEnabled = Value

        End Set
    End Property

What this accomplishes is the Enable property is a nested property of
LeftButton1.

So when another section of code using this web control performs the
following statement:

oPageFooter.LeftButton1.Enabled = true

And this statement is executed in an event of a web page (which means the
CreateChildControls has already been called for the PageFooter class), I need
to be able to update the control collection in the PageFooter class from the
LeftButton1Properties class.

If I can't do this, the web control displayed on the web page does not
correctly reflect the current state of the enable property.

"Steven Cheng[MSFT]" wrote:

> Hi,
>
> Thanks for posting here. As for the problems you mentioned, here are some
> of my sugestions:
>
> 1. In the ASP.NET custom web control, if we can making a composite control,
> we create the control
> collection in the CreateChildControls method. Also, we can access the
> Control's collection everywehere in the
> control's code via Me.Controls property, but we are recommend to only
> construct the control hierarchy in the
> CreateChildControls method.
>
> 2. As for the "accessing and modifying the control collection when a
> certain propery is modified", I think we can use
> such approach, in the property's "Set" section, we set the control's
> "ChildControlCreated" as false and manually call the
> CreateChildControls to reconstructed the control hierarchy. For example:
>
> Set(ByVal Value As Boolean)
> _mode = Value
> Me.ChildControlsCreated = False
> Me.CreateChildControls()
> End Set
>
> And here is a simple sample using the above means:
> ==============================
> <DefaultProperty("Text"), ToolboxData("<{0}:CustomControl
> runat=server></{0}:CustomControl>")> Public Class CustomControl
> Inherits System.Web.UI.WebControls.WebControl
>
> Dim _text As String
> Dim _mode As Boolean
>
> <Bindable(True), Category("Appearance"), DefaultValue("")> Property
> [Text]() As String
> Get
> Return _text
> End Get
>
> Set(ByVal Value As String)
> _text = Value
>
>
> End Set
> End Property
>
> <Bindable(True), Category("Appearance"), DefaultValue(True)> Property
> Mode() As Boolean
> Get
> Return _mode
> End Get
>
> Set(ByVal Value As Boolean)
> _mode = Value
> Me.ChildControlsCreated = False
> Me.CreateChildControls()
> End Set
> End Property
>
>
> 'Protected Overrides Sub Render(ByVal output As
> System.Web.UI.HtmlTextWriter)
> ' output.Write([Text])
> 'End Sub
>
> Protected Overrides Sub CreateChildControls()
> Me.Controls.Clear()
>
> Dim txt As New TextBox
> txt.ID = "txtContent"
>
> If Mode Then
> txt.Text = "Mode: On"
> Else
> txt.Text = "Mode: Off"
> End If
> Me.Controls.Add(txt)
> End Sub
>
> Protected Overrides Sub LoadViewState(ByVal savedState As Object)
> Dim data As System.Web.UI.Triplet = savedState
> Mode = data.First
> MyBase.LoadViewState(data.Second)
>
> End Sub
>
> Protected Overrides Function SaveViewState() As Object
> Dim data As New Triplet
> data.First = Mode
> data.Second = MyBase.SaveViewState()
> End Function
> End Class
> ===============================================
>
> In addition you can also try directly accessing the "Me.Controls"
> collection when the propety is
> modified, but this need you make sure you've do the correct changing in
> your control collection so as not to make it corrupted.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
> Get Preview at ASP.NET whidbey
> http://msdn.microsoft.com/asp.net/whidbey/default.aspx
>
>


Quantcast