Question: EnsureChildControls Method in CompositeControl

來源:互聯網
上載者:User

來源:http://www.imcoder.org/custom-control/189732.htm

 

Q:

I don't get it... what's the objective of EnsureChildControls method in a CompositeControl class properties? There's no difference if I use it or not!

    Public Property Required() As Boolean
        Get
            EnsureChildControls()
            Return _Required
        End Get
        Set(ByVal value As Boolean)
            EnsureChildControls()
            _Required = value
        End Set
    End Property

there's no difference using

    Public Property Required() As Boolean
        Get
            Return _Required
        End Get
        Set(ByVal value As Boolean)
            _Required = value
        End Set
    End Property

A:

Let me cite you from [1, p.924]

"In ASP.NET 1.x, the process for creating a composite control was subtly different. No CompositeControl  class existed, so you had to derive from the WebControl class yourself. However, there are only two differences between CompositeControl and WebControl. First, CompositeControl implements INamingContainer so all the child controls are uniquely scoped and their IDs won’t conflict with page controls or other instances of your composite control. Second, CompositeControl calls the EnsureChildControls() method automatically when you access the Controls collection, which makes sure child controls are created before you try to manipulate them."

So, apprently there is no difference

[1]
Matthew MacDonald and Mario Szpuszta
Pro ASP.NET 2.0 in C# 2005 A:

EnsureChildControls makes sure child controls are created prior to accessing them.  For example, if you had a property such as the following…

 

  Public Class MySampleControl

        Inherits System.Web.UI.WebControls.CompositeControl

        ' This sample is only meant to show why EnsureChildControls can be necessary.

 

        Protected WithEvents FirstNameTextBox As System.Web.UI.WebControls.TextBox

 

        Public Property FirstName() As String

            Get

                ' Will call CreateChildControls if needed

                EnsureChildControls()

                Return FirstNameTextBox.Text

            End Get

            Set(ByVal value As String)

                ' Will call CreateChildControls if needed

                EnsureChildControls()

                FirstNameTextBox.Text = value

            End Set

        End Property

 

        Protected Overrides Sub CreateChildControls()

            MyBase.CreateChildControls()

            FirstNameTextBox = New System.Web.UI.WebControls.TextBox

            FirstNameTextBox.ID = "FirstNameTextBox"

            Controls.Add(FirstNameTextBox)

        End Sub

 

 

    End Class

 

 

 

    ' Some page/user control class...

    Public Sub CreateSampleControl()

        Dim ctl As New MySampleControl()

        ' EnsureChildControls makes sure the property will

        ' not fail when accessed before default call to CreateChildControls

        ctl.FirstName = "Brian"

        Controls.Add(ctl)

 

    End Sub

A:

Yet Microsoft's own post on the topic indicates that if you are using the new CompositeControl in ASP.NET 2.0 you no longer have to explicitly call EnsureChildControls():

The CompositeControl class is new in ASP.NET 2.0. If you created custom controls in ASP.NET version 1.0 or 1.1, you had to implement the INamingContainer interface to create a new naming scope for child controls. In addition, you had to override the Controls property and invoke the EnsureChildControls method. In ASP.NET 2.0, these and other steps are performed by the CompositeControl class.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.