문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top