Question

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?

Was it helpful?

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);
    }

OTHER TIPS

Here is what you can do: DisplayMode Property

ListItem item = new ListItem("MicroSoft", "http://www.microsoft.com");
ListItem item1 = new ListItem("Google", "http://www.google.com");
BulletedList1.Items.Add(item);
BulletedList1.Items.Add(item1);

ListItem is not a container hence does not provide controls to be added. Use ListViewDataItem instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top