Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Inheriting System.UI.ControlCollection for a WebControl - FindControl doesn't work???

Thread Tools

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

Harry F. Harrison
Posts: n/a
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.


Dim TextBox as System.Web.UI.WebControls.TextBox
1..TextBox = New System.Web.UI.WebControls.TextBox
2..TextBox.ID = "myTextBox"

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")


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
'breaks on this method - FindControl returns Nothing


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

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




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)


End Sub

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

child.ID = ID


End Sub

End Class

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Access Base Members when Inheriting WebControl TJO ASP .Net 2 03-16-2006 02:35 PM
Trapping an Exception ocurring in a Webcontrol at the Page or parent WebControl level. ASP .Net 2 01-31-2005 06:34 PM
WebControl inside of WebControl and attaching events Kasabaarde Sumta ASP .Net Building Controls 0 12-13-2004 09:51 PM
CodeAcces Security + Inheriting WebControl Rudi van den Belt ASP .Net Web Controls 0 10-25-2004 11:31 AM
Can a webcontrol (webcontrol.dll) have a configuration file? Luis Ramírez. ASP .Net Building Controls 2 07-06-2004 04:35 PM