以编程方式将 InnerHtml 添加到 ListItem?

发布于 2024-11-26 22:29:04 字数 1251 浏览 1 评论 0原文

我有一个服务器控件,它包装了一个呈现为无序列表的 BulletedList。

我希望能够将 ListItem 文本中的 HTML 标签呈现为 HTML(不转义,就像现在一样)。

我开始认为这是不可能的。我在这里看到了几个类似的问题,但他们的答案都涉及在aspx页面中使用eg中继器,而我没有。 我知道某些控件具有不同的属性来按字面形式呈现内容或转义内容,例如 Text/InnerHTML,但我找不到 BulletedList 的任何类似可能性。

当然它一定是可能的...

这是后面的代码中的代码,它使用我的服务器控件(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>"));
    }
}

这是我的服务器列表控件的相关部分:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-03 22:29:04

项目符号列表似乎根本不支持插入 HTML。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文