Search code examples
asp.nethtmlgenericcontrol

adding children to html generic control asp.net


I'm trying to programmatically create an unordered list using the asp.net HTMLGenericControl.

It properly creates the "ul" parent, but the "li" children don't get created/added properly - they just get wrapped in the default "span" tags. Obviously I'm doing something wrong, but my logic was to try to first create the hyperlinks, then add those to the "li" control set, then try to add the entire "li" set to the "ul" control.

Here is my code:

Private Sub CreateTabButtons()

    pnlSideMenuItems.Controls.Clear()

    Dim objLink As HyperLink
    Dim objUnorderedListItem As New HtmlGenericControl("li")
    Dim objUnorderedList As New HtmlGenericControl("ul")

    For Each TabItem As TabDescriptor In TabDescriptors()

        objLink = New HyperLink()
        objUnorderedListItem = New HtmlGenericControl()
        objLink.NavigateUrl = "javascript:void(0)"
        objLink.ID = String.Format("link_{0}", TabItem.PanelId)

        If TabItem.IsEnabled Then
            objLink.CssClass = "enabled"
            objLink.AccessKey = TabItem.AccessKey
        Else
            objLink.CssClass = "disabled"
        End If

        objLink.Enabled = TabItem.IsEnabled
        objLink.Text = TabItem.Title
        //create <li> items from hyperlinks
        objUnorderedListItem.Controls.Add(objLink)
        //add <li> items to <ul> control
        objUnorderedList.Controls.Add(objUnorderedListItem)                        
    Next
    //after loop exit, add the entire unordered list control to the panel        
    pnlSideMenuItems.Controls.Add(objUnorderedList) 
End Sub

Any help would be greatly appreciated.


Solution

  • Nevermind - I realized I needed to declare a tagname for the control:

    objUnorderedListItem.TagName = "li"

    Which now works correctly...stupid mistake. Thanks all.