Inheriting System.UI.ControlCollection for a WebControl - FindControl doesn't work???

Discussion in 'ASP .Net' started by Harry F. Harrison, Dec 18, 2003.

  1. Here's the situation...Context - Using an Inherited WebControl object as a
    base for server custom controls.

    In a web custom control, whenever I create a child control and add it to the
    Controls class, it takes 3 lines of code.

    Example:

    Dim TextBox as System.Web.UI.WebControls.TextBox
    1..TextBox = New System.Web.UI.WebControls.TextBox
    2..TextBox.ID = "myTextBox"
    3..Controls.Add(TextBox)

    I would like to overload the Controls object in WebControl to do this in one
    line of code.

    Controls.Add(New System.Web.UI.WebControls.TextBox, "myTextBox")

    HOWEVER...

    I don't think I've got everything glued together. For example: FindControl
    doesn't return any controls that exist in my custom object. It is still
    looking at myBase.Controls (I think). If I need to override FindControl,
    are there other methods that I need to override as well, or am I going down
    the wrong path?

    'sample code
    Public Class MyControl

    Inherits CompositeWebControl

    Protected Overrides Sub CreateChildControls()
    Controls.Add(New System.Web.UI.WebControls.TextBox, "myTextBox")

    End Sub

    Protected Overrides Sub Render(ByVal writer As
    System.Web.UI.HtmlTextWriter)
    'breaks on this method - FindControl returns Nothing

    FindControl("myTextBox").RenderControl(writer)

    End Sub

    End Class


    Option Explicit On
    Option Strict On

    'inherited WebControl - use as a base for new WebControls

    Public Class CompositeWebControl
    Inherits System.Web.UI.WebControls.WebControl

    Implements System.Web.UI.INamingContainer

    Private mobjControls As WebControlCollection

    Protected Overrides Function CreateControlCollection() As
    System.Web.UI.ControlCollection

    If (mobjControls Is Nothing) Then

    mobjControls = New WebControlCollection(Me)

    End If

    Return CType(mobjControls, System.Web.UI.ControlCollection)

    End Function

    <System.ComponentModel.Browsable(False)> _

    Public Shadows ReadOnly Property Controls() As WebControlCollection

    Get

    Me.CreateControlCollection()

    Me.EnsureChildControls()

    Return mobjControls

    End Get

    End Property

    End Class

    'custom ControlCollection that has an overloaded Add method...

    Public Class WebControlCollection
    Inherits System.Web.UI.ControlCollection

    Sub New(ByVal owner As System.Web.UI.Control)

    MyBase.New(owner)

    End Sub

    Public Overloads Sub Add(ByVal child As System.Web.UI.Control, ByVal ID
    As String)

    child.ID = ID

    Me.Add(child)

    End Sub

    End Class
     
    Harry F. Harrison, Dec 18, 2003
    #1
    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. DanielSchaffer

    Inheriting from ControlCollection

    DanielSchaffer, Mar 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    462
    Peter Rilling
    Mar 8, 2005
  2. =?Utf-8?B?R3Vyc2hhcmFu?=

    System.Web.UI.ControlCollection exception

    =?Utf-8?B?R3Vyc2hhcmFu?=, Jul 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    721
    =?Utf-8?B?R3Vyc2hhcmFu?=
    Jul 31, 2006
  3. Replies:
    1
    Views:
    323
    Mark Fitzpatrick
    Nov 28, 2006
  4. Mark

    Custom control collections - IList or ControlCollection

    Mark, Apr 27, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    328
  5. Abraham Andres Luna

    ControlCollection.Remove bug

    Abraham Andres Luna, Oct 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    324
    Alvin Chooi
    Oct 31, 2006
Loading...

Share This Page