Search code examples
asp.netwebformsweb-controls

How To Create an Anchor In a List Item in ASP.NET WebForms


I want to create nested tags like this:

<ul>
    <li><a href="">xxx</a></li>
</ul>

Then I add a BulletedList control in the page:

<asp:BulletedList runat="server" CssClass="ad-thumb-list" ID="ImageItem">
</asp:BulletedList>

Then I add the ListItem in the code :

    DataTable dt = queryDb(xx);
    foreach (DataRow dr in dt.Rows)
    {
        HyperLink hl = new HyperLink();
        hl.NavigateUrl = string.Format("../stream.aspx?type=={0}",dr["type"]);

        ListItem li = new ListItem();
        //here how to add the `hl` to `li`
        ImageItem.Items.Add(li);
    }

In the above code,I have no idea to add the a tag to li.

I want to use something like this:

li.Controls.Add(hl); 

But it seems taht this is impossible.

What is the problem?


Solution

  • You need to set DisplayMode has Hyperlink (BulletedListDisplayMode.HyperLink)

        ImageItem.DisplayMode = BulletedListDisplayMode.HyperLink;
    
        foreach (DataRow dr in dt.Rows)
        {
            ListItem li = new ListItem("PLACE-THE-LINK-TEXT", string.Format("../stream.aspx?type=={0}",dr["type"]));
            //here how to add the `hl` to `li`
            ImageItem.Items.Add(li);
        }