Control. Please, does anyone knows how to solve this? Thank You!

Discussion in 'ASP .Net' started by shapper, May 30, 2007.

  1. shapper

    shapper Guest

    Hello,

    I want to create an ordered list where each list item can have various
    controls:

    <ol>
    <li>textbox, label, ...</li>
    ...
    </ol>

    I created two classes: List and ListItem
    List class renders as <ol> and I add ListItems to it, as a
    Generic.List(Of ListItem)
    ListItem rendes as <li> and I add web controls (textbox, label, etc)
    to it, as a Generic.List(Of WebControl)

    For the past 4 days I tried everything I could think of but until now
    I wasn't able to make this work.

    As far as I can understand the Generic.Lists are not working since
    they always count 0.

    Could someone, please, help me? I really don't know what else to do.

    Here is my full code:

    1 ' List
    2 <DefaultProperty("ID"), ToolboxData("<{0}:List runat=server></
    {0}:List>")> _
    3 Public Class List
    4 Inherits WebControl
    5 Implements INamingContainer
    6
    7 ' Items ...
    8 <Bindable(True), Category("Settings"), DefaultValue(""),
    Localizable(True), PersistenceMode(PersistenceMode.InnerProperty)> _
    9 Property Items() As Generic.List(Of ListItem)
    10 Get
    11 If ViewState("Items") Is Nothing Then
    12 Return New Generic.List(Of ListItem)
    13 Else
    14 Return ViewState("Items")
    15 End If
    16 End Get
    17 Set(ByVal Value As Generic.List(Of ListItem))
    18 ViewState("Items") = Value
    19 End Set
    20 End Property ' Items
    21
    22
    23
    24
    25 ' New
    26 Public Sub New()
    27 End Sub ' New
    28
    29 ' Add
    30 Public Sub Add(ByVal item As ListItem)
    31
    32 ' Add item to items
    33 Me.Items.Add(item)
    34
    35 End Sub ' Add
    36
    37 ' CreateChildControls
    38 Protected Overrides Sub CreateChildControls()
    39
    40 ' Add controls to item
    41 For Each item As ListItem In Me.Items
    42 MyBase.Controls.Add(item)
    43 Next ' item
    44
    45 HttpContext.Current.Response.Write("> List Items:" &
    Me.Items.Count.ToString)
    46
    47 ' Create child controls
    48 MyBase.CreateChildControls()
    49 Me.ChildControlsCreated = True
    50
    51 End Sub ' CreateChildControls
    52
    53 ' RenderBeginTag
    54 Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
    HtmlTextWriter)
    55
    56 ' Add control attributes
    57 With writer
    58 If Not String.IsNullOrEmpty(Me.ID)
    Then .AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
    59 If Not String.IsNullOrEmpty(Me.CssClass)
    Then .AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
    60 End With ' writer
    61
    62 ' Render begin tag
    63 writer.RenderBeginTag(HtmlTextWriterTag.Ol)
    64
    65 End Sub ' RenderBeginTag
    66
    67 ' RenderEndTag
    68 Public Overloads Overrides Sub RenderEndTag(ByVal writer As
    HtmlTextWriter)
    69
    70 ' Render end tag
    71 writer.RenderEndTag()
    72
    73 End Sub ' RenderEndTag
    74
    75 End Class ' List
    76
    77 ' ListItem
    78 <DefaultProperty("ID"), ToolboxData("<{0}:ListItem
    runat=server></{0}:ListItem>")> _
    79 Public Class ListItem
    80 Inherits WebControl
    81 Implements INamingContainer
    82
    83 ' WebControls ...
    84 <Bindable(True), Category("Settings"), DefaultValue(""),
    Localizable(True), PersistenceMode(PersistenceMode.InnerProperty)> _
    85 Property WebControls() As Generic.List(Of WebControl)
    86 Get
    87 If ViewState("WebControls") Is Nothing Then
    88 Return New Generic.List(Of WebControl)
    89 Else
    90 Return ViewState("WebControls")
    91 End If
    92 End Get
    93 Set(ByVal Value As Generic.List(Of WebControl))
    94 ViewState("WebControls") = Value
    95 End Set
    96 End Property ' WebControls
    97
    98 ' New
    99 Public Sub New()
    100 End Sub ' New
    101
    102 ' New
    103 Public Sub New(ByVal id As String, ByVal controls() As
    WebControl)
    104
    105 ' Define control properties
    106 Me.ID = id
    107 Me.WebControls = New System.Collections.Generic.List(Of
    WebControl)(controls)
    108
    109 End Sub ' New
    110
    111 ' CreateChildControls
    112 Protected Overrides Sub CreateChildControls()
    113
    114 ' Add controls to item
    115 For Each control As WebControl In Me.WebControls
    116 MyBase.Controls.Add(control)
    117 Next ' control
    118
    119 HttpContext.Current.Response.Write("> Web Controls Count:"
    & Me.WebControls.Count.ToString)
    120
    121 ' Create child controls
    122 MyBase.CreateChildControls()
    123 Me.ChildControlsCreated = True
    124
    125 End Sub ' CreateChildControls
    126
    127 ' RenderBeginTag
    128 Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
    HtmlTextWriter)
    129
    130 ' Add control attributes
    131 With writer
    132 If Not String.IsNullOrEmpty(Me.ID)
    Then .AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
    133 If Not String.IsNullOrEmpty(Me.CssClass)
    Then .AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
    134 End With ' writer
    135
    136 ' Render begin tag
    137 writer.RenderBeginTag(HtmlTextWriterTag.Li)
    138
    139 End Sub ' RenderBeginTag
    140
    141 ' RenderEndTag
    142 Public Overloads Overrides Sub RenderEndTag(ByVal writer As
    HtmlTextWriter)
    143
    144 ' Render end tag
    145 writer.RenderEndTag()
    146
    147 End Sub ' RenderEndTag
    148
    149 End Class ' ListItem


    And I am using it as follows:

    1 Dim myList As List
    2
    3 With myList.Items
    4 .Add(New ListItem("liName", New WebControl() {lName,
    tbName}))
    5 ...
    6 End With

    Thanks,

    Miguel
     
    shapper, May 30, 2007
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.