编程添加一个超链接到未DISPLAYMODE =超链接的项目符号列表 [英] Programmatically adding a hyperlink to a bulleted list that IS NOT DisplayMode=Hyperlink

查看:185
本文介绍了编程添加一个超链接到未DISPLAYMODE =超链接的项目符号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,直到今天,被创造只为纯文本使用ASP.NET项目符号列表控制。新的设计要求问,我把部分这些项目为超链接。因此,项目符号列表将最终需要包含一些纯文本的项目,以及一些超链接。如果我将其更改为DISPLAYMODE =超链接,即使我离开值为空,该条目应该仅仅是纯文本还是成为可点击的链接。

I have a ASP.NET bulleted list control that, until today, was created and used only for plain text. A new design request asks that I turn SOME of those items into hyperlinks. Therefore the bulleted list will ultimately need to contain some plain text items, and some hyperlinks. If I change it to DisplayMode=Hyperlink, even if I leave the value blank, the entries that should just be plain text still become clickable links.

一个解决方案,我认为我可以做的工作,就是用文字控制和使用HTML,在需要的线(&LT A HREF ... )要链接。这将需要重新工作的一些老code一点点,所以我才尝试,我真的很想知道这是否可能与现有的BulletedList做。

One solution that I think I can make work, is to use a Literal control and use HTML (<a href...) on the lines that need to be links. That will entail a little bit of re-working some old code, so before I try that I really want to know if this is possible to do with the existing BulletedList.

编辑:

我认真找不到这个在任何地方任何事情,我一般认为自己是pretty良好的Google用户。所以对于一个或两个失落和迷惘的灵魂谁发现自己在某个时候在未来十年相同的情况下,这里是下面我提供优良的答案完全实现:

I seriously couldn't find anything about this anywhere, and I generally consider myself a pretty good Googler. So for the one or two lost and confused souls who find themselves in the same scenario sometime in the next decade, here is my complete implementation of the excellent answer offered below:

在页面的code-背后:

In the page's code-behind:

foreach (SupportLog x in ordered)
{
    blschedule.Items.Add(new ListItem(x.Headline, "http://mysite/Support/editsupportlog.aspx?SupportLogID=" + x.SupportLogID));
}

blschedule.DataBind();

注意的DataBind末---这是必要的落入DataBound事件:

Note the DataBind at the end --- this is necessary to fall into the DataBound event:

protected void blschedule_DataBound(object sender, EventArgs e)
{
    foreach (ListItem x in blschedule.Items)
    {
        if (x.Value.Contains("http")) //an item that should be a link is gonna have http in it, so check for that
        {
            x.Attributes.Add("data-url", x.Value);
        }
    }
}

在.aspx页面中的头:

In the .aspx page's head:

<script src="<%# ResolveClientUrl("~/jquery/jquery141.js") %>" type="text/javascript"></script>
    <script>

        $(document).ready(function () {

           $('#<%=blschedule.ClientID %> li').each(function () {
               var $this = $(this);
               var attr = $this.attr('data-url');

               if (typeof attr !== 'undefined' && attr !== false) {
                   $this.html('<a href="' + $this.attr('data-url') + '">' + $this.text() + '</a>');
               }
           });
       });

    </script>

,若需要语句,以确保只把有数据链接中的项目属性为链接,而不是把所有物品进入链接。

The if statement is required to make sure to only turn the items that have the "data-url" attribute into links, and not turn ALL items into links.

推荐答案

您可能会发现它更容易使用的 &LT; ASP:直放站/&GT; 该任务

You may find it's easier to use an <asp:Repeater /> for that task.

是这样的:

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>
        <li><%# string.IsNullOrEmpty(Eval("url").ToString()) ? Eval("text") : string.Format("<a href=\"{0}\">{1}</a>", Eval("url").ToString(), Eval("text").ToString()) %></li>
    </ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Hackalicious路

设置URL值 DataValueField 时,数据绑定的BulletedList

set the URL value to DataValueField when data binding the BulletedList

使用数据绑定事件通过项目来迭代,并用URL值添加到每一个属性

use the DataBound event to iterate through the items and add an attribute to each one with a URL value

protected void BulletedList1_DataBound(object sender, EventArgs e)
{
    foreach (ListItem i in BulletedList1.Items)
    {
        if (i.Value.Length > 0)
        {
            i.Attributes.Add("data-url", i.Value);
        }
    }
}

使用JavaScript / jQuery的采取必要的标记:

use JavaScript/jQuery to apply the necessary markup:

$('[data-url]').each(function() {
    var $this = $(this);
    $this.html('<a href="' + $this.attr('data-url') + '">' + $this.text() + '</a>');
});

没有测试这个jQuery,但它应该接近

didn't test this jQuery but it should be close

这篇关于编程添加一个超链接到未DISPLAYMODE =超链接的项目符号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆