netsmithcentral
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:
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.
Is there some reason I'm missing to use the EnsureChildControls model instead of the property accessor model?
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
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
Is there some reason I'm missing to use the EnsureChildControls model instead of the property accessor model?
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).