Avatar of netsmithcentral
netsmithcentral
Flag for United States of America asked on

Where to create child control objects - property accessors (get) or CreateChildControls

I know the .NET system is designed for custom controls to create their child control methods in the CreateChildControls method, and then call EnsureChildControls() before accessing any of their properties, like the following example:
Protected Overrides Sub CreateChildControls()
            _txtSearch = New TextBox()
            Me.Controls.Add(_txtSearch)
        End Sub

        Public Property Text As String
            Get
                EnsureChildControls()
                Return _txtSearch.Text
            End Get
            Set(ByVal value As String)
                EnsureChildControls()
                _txtSearch.Text = value
            End Set
        End Property

Open in new window


I don't see why this is necessary.  Couldn't we just use a property accessor model, create controls there, and add them to the controls collection in CreateChildControls?  That would eliminate the need for using/remembering to call EnsureChildControls() on property access!  FYI, I have tested this method and it works fine.
Private _txtSearch As TextBox = Nothing
        Private ReadOnly Property txtSearch As TextBox
            Get
                If _txtSearch Is Nothing Then
                    _txtSearch = New TextBox()
                End If

                Return _txtSearch
            End Get
        End Property

        Protected Overrides Sub CreateChildControls()
            Me.Controls.Add(txtSearch)
        End Sub

Open in new window


Is there some reason I'm missing to use the EnsureChildControls model instead of the property accessor model?
.NET ProgrammingVisual Basic.NETASP.NET

Avatar of undefined
Last Comment
Member_6283346

8/22/2022 - Mon
Member_6283346

Hi! CreateChildControl is not for creating controls like objects, but place for adding child controls in control tree (Controls collection). Because if you add controls to control tree too late (after load event) they can behave unexpectedly.
For example you can init your textbox just when you declare it and then add it to Controls in CreateChildControls:

Dim _txtSearch As TextBox = New TextBox

Protected Overrides Sub CreateChildControls()
            Me.Controls.Add(_txtSearch)
        End Sub

EnsureChildControls in just way to make sure that CreateChildControls was called, it is sometimes used in properties, as you mentioned, but it is not necessary (because sooner or later CreateChildControl will called automatically).
Irzana

Hi,

Say if you want to set the Text of the text box from outside, how can you do that with Property accessor model? again you have to call something like this,

Public Property Text As String
         
            Set(ByVal value As String)
               Me.EnsureChildControls()
               DirectCast(Controls(1), TextBox).Text = value.ToString()
            End Set
        End Property

netsmithcentral

ASKER
I use the property accessor model on my controls, not on their properties.  Then, if I want to access the values in code, I call the control property like so (notice the second code example in my original post has a Private modifier on the property):
'The internal control property initialized with the property
        'accessor model
        Private _txtSearch As TextBox = Nothing
        Private ReadOnly Property txtSearch As TextBox
            Get
                If _txtSearch Is Nothing Then
                    _txtSearch = New TextBox()
                End If

                Return _txtSearch
            End Get
        End Property

        'The external text property
        Public Property Text As String
            Get
                Return txtSearch.Text
            End Get
            Set(value As String)
                txtSearch.Text = value
            End Set
        End Property

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Member_6283346

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question