使用Listview标签的C#解决方案
我想使用此解决方案将URL转换为listview标签中的链接使用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)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; Sys
private string ConvertUrlsToLinks(string text)
{
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
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=\"_blank\">$1</a>").Replace("href=\"www", "href=\"http://www");
}
谢谢根据您的代码,您使用的是ASP.NET Web表单,而不是MVC
ProjectPostLabel
实例与Text
属性一起使用-无需创建未分配到任何位置的新标签URL
属性的NorthwindEmployee
类。必须将其强制转换到列表视图中使用的自己的类
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).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));
}