Search code examples
c#.netasp.netservercontrols

Add InnerHtml to a ListItem programatically?


I have a server control that wraps a BulletedList which renders as an unordered list.

I would like to be able to render HTML tags in the text of a ListItem as HTML (not escaped, like now).

I'm beginning to think it's not possible. I've seen several similar questions here, but their answers all involve the use of e.g. repeaters in the aspx page, which I don't have. I'm aware that some controls have different properties for rendering the content literally or escaped, such as Text/InnerHTML, but I can't find any similar possibilities for BulletedList.

Surely it must be possible...

Here is the code in the code behind which uses my server control ( list)

protected void RenderItem(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        e.Item.Controls.Add(new LiteralControl("<li >"));

        Relation rel = (Relation)e.Item.DataItem;
        // rel.List is the control containing the BulletedList
        MyList list = rel.List;
        e.Item.Controls.Add(list); 

        e.Item.Controls.Add(new LiteralControl("</li>"));
    }
}

Here's the relevant part of my server list control:

public class MyList : Control, INamingContainer
{

    private readonly HyperLink _showAllLink = new HyperLink { NavigateUrl = "javascript:void(0);", Visible = false };
    private readonly BulletedList _list = new BulletedList();

    public ListItemCollection Items
    {
        get
        {
             return _list.Items;
        }
    }

}


Solution

  • It doesn't seem like Bulleted list supports insertion of HTML at all.