On May 28, 7:38 pm, shapper <mdmo...@gmail.com> wrote:
> Hello,
>
> I have a class where I created various controls.
> One of the controls have a property which is a generic list of
> WebControl.
>
> Then in web site page I have something like:
> Dim a As New MyNamespace.ListItem
> a.WebControls.Add(tbName)
> a.WebControls.Add(lLabel)
> Response.Write(a.WebControls.Count) >>> This returns 0!
>
> When the control renders I can only see its start and end tags.
> The controls that I added to it are not rendered into the page.
> In fact they seem to not being added to the WebControls property.
> (Count returns 0)
>
> I am on this for 2 days. I have no idea what is going on. I tried
> everything I could think of!
> I think the problem might be in my control property WebControls.
>
> Please, could someone help me out?
>
> Here is my control code:
>
> <DefaultProperty("ID"), ToolboxData("<{0}:Item runat=server></
> {0}:Item>")> _
> Public Class ListItem
> Inherits WebControl
> Implements INamingContainer
>
> <Bindable(True), Category("Settings"), DefaultValue(""),
> Localizable(True)> _
> Property WebControls() As Generic.List(Of WebControl)
> Get
> If CStr(ViewState("WebControls")) Is Nothing Then
> Return New Generic.List(Of WebControl)
> Else
> Return ViewState("WebControls")
> End If
> End Get
> Set(ByVal Value As Generic.List(Of WebControl))
> ViewState("WebControls") = Value
> End Set
> End Property ' WebControls
>
> ' CreateChildControls
> Protected Overrides Sub CreateChildControls()
>
> ' Add controls to item
> For Each control As WebControl In Me.WebControls
> MyBase.Controls.Add(control)
> Next ' control
>
> ' Create child controls
> MyBase.CreateChildControls()
> Me.ChildControlsCreated = True
>
> End Sub ' CreateChildControls
>
> End Class ' List
>
> Thanks,
> Miguel
Please, anyone?
My full code is as follows:
1
2 <DefaultProperty("ID"), ToolboxData("<{0}:ListItem
runat=server></{0}:ListItem>")> _
3 Public Class ListItem
4 Inherits WebControl
5 Implements INamingContainer
6
7 <Bindable(True), Category("Settings"), DefaultValue(""),
Localizable(True)> _
8 Property WebControls() As Generic.List(Of WebControl)
9 Get
10 If CStr(ViewState("WebControls")) Is Nothing Then
11 Return New Generic.List(Of WebControl)
12 Else
13 Return ViewState("WebControls")
14 End If
15 End Get
16 Set(ByVal Value As Generic.List(Of WebControl))
17 ViewState("WebControls") = Value
18 End Set
19 End Property ' WebControls
20
21 Protected Overrides Sub CreateChildControls()
22
23 ' Add controls to item
24 For Each control As WebControl In Me.WebControls
25 MyBase.Controls.Add(control)
26 Next ' control
27
28 ' Create child controls
29 MyBase.CreateChildControls()
30 Me.ChildControlsCreated = True
31
32 End Sub ' CreateChildControls
33 End Class ' ListItem
34
35 ' List
36 <DefaultProperty("ID"), ToolboxData("<{0}:List runat=server></
{0}:List>")> _
37 Public Class List
38 Inherits WebControl
39 Implements INamingContainer
40
41 ' Items ...
42 <Bindable(True), Category("Settings"), DefaultValue(""),
Localizable(True)> _
43 Property Items() As Generic.List(Of ListItem)
44 Get
45 If CStr(ViewState("Items")) Is Nothing Then
46 Return New Generic.List(Of ListItem)
47 Else
48 Return ViewState("Items")
49 End If
50 End Get
51 Set(ByVal Value As Generic.List(Of ListItem))
52 ViewState("Item") = Value
53 End Set
54 End Property ' Items
55
56 ' CreateChildControls
57 Protected Overrides Sub CreateChildControls()
58
59 ' Add controls to item
60 For Each item As ListItem In Me.Items
61 MyBase.Controls.Add(item)
62 Next ' item
63
64 ' Create child controls
65 MyBase.CreateChildControls()
66 Me.ChildControlsCreated = True
67
68 End Sub ' CreateChildControls
69
70 End Class ' List
Thanks,
Miguel
|