使用Listview标签的C#解决方案

使用Listview标签的C#解决方案,c#,asp.net,listview,C#,Asp.net,Listview,我想使用此解决方案将URL转换为listview标签中的链接 private string ConvertUrlsToLinks(string text) { string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; Sys

我想使用此解决方案将URL转换为listview标签中的链接

private string ConvertUrlsToLinks(string text)
{
   string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
    System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    return r.Replace(text, "<a href=\"$1\" title=\"Open in a new window or tab\" target=\"&#95;blank\">$1</a>").Replace("href=\"www", "href=\"http://www");
}

谢谢

根据您的代码,您使用的是ASP.NET Web表单,而不是MVC

  • 您必须将
    ProjectPostLabel
    实例与
    Text
    属性一起使用-无需创建未分配到任何位置的新标签
  • 必须从事件中检索Url属性,而不是label控件。我在我的ListView中使用了带有
    URL
    属性的
    NorthwindEmployee
    类。必须将其强制转换到列表视图中使用的自己的类

    protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
    }
    
  • 您必须记住,标签中只会显示列表视图中的最后一项(除非您预期会出现这种行为)。如果要从列表中列出URL,可以编写以下内容:

        protected void Page_Load(object sender, EventArgs e)
        {
            ProjectPostLabel.Text = string.Empty;
        }
    
        protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
        }
    
    受保护的无效页面加载(对象发送方,事件参数e)
    {
    ProjectPostLabel.Text=string.Empty;
    }
    受保护的void projectRecentActivityListView\u ItemDataBound(对象发送方,ListViewItemEventArgs e)
    {
    ProjectPostLabel.Text+=string.Format(“{0}
    ”,ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem.URL)); }
    您的函数返回一个字符串。不能将其指定给标签。您可以转到ProjectPostLabel.Text=。。。但我不确定这是否会达到你的预期(把它变成锚)。MVC从何而来?
        protected void Page_Load(object sender, EventArgs e)
        {
            ProjectPostLabel.Text = string.Empty;
        }
    
        protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
        }