从asp.net c中的数据库列获取超链接列表

时间:2013-06-08 09:54:20

标签: c# asp.net database hyperlink label

我有一个项目网站(报价网站) 我有一个页面,其中显示所有报价以收集。 (截图如下)。 我使用存储过程来显示这个结果。

enter image description here

现在我的主要问题是:

正如您所看到的,标签有一个字段 例如:标签:生活,搞笑,领导力,鼓舞人心,友谊

但是这个值来自数据库列类别。我正在使用一个标签来显示所有类别。

但我想将其划分为个别类别,并将用户重定向到他点击的特定类别。

我有什么办法可以在多个超链接中划分这个单一标签吗?

我的存储过程(以防任何需要)

ALTER Proc text_quotes
    (
    @cat varchar(50)
    )
as
begin
    select p.id,p.title,p.description,p.category,p.metadescp,p.metatitle,p.tags,f.img
    from tbl_upload_image p
    inner join tbl_author f
    on p.description = f.name
    where p.category like '%' + @cat + '%'
    order by p.upload_date desc
end

数据库中的示例条目:

id:12

引用:dghjn

类别:生活,有趣,领导,鼓舞人心,友谊

作者:Jim Carrey

更新:

我使用了以下编码:

foreach (DataListItem item in DataList4.Items)
        {
            Repeater RepeaterQ = ((Repeater)(item.FindControl("Repeater1")));
            string categories = ((Label)(item.FindControl("categoryLabel"))).ToString();

            // Label lblCategory = ((Label)(DataList4.FindControl("categoryLabel")));
            string[] arr1 = categories.Split(',');

            RepeaterQ.DataSource = arr1;

            RepeaterQ.DataBind();
        }

但我得到" System.Web.UI.WebControls.Label"作为转发器中的文本。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

如果您的类别数据位于categories之类的变量中,那么我认为您正在分配标签lblCategory作为示例。

lblCategory.Text = categories;

如果您这样做,那么您可以试试这个。

lblCategory.Text = String.Join("", 
     categories.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries) //Split Category csv to array of categories
    .Select(x=>String.Format("<a href='page.aspx?cat={0}'>{0}</a>"), x)) // return all categories with formatted anchor tag
    .ToArray() // Convert To Array
); //Join with empty string and assign to label text property

首先用逗号分隔您的类别,然后使用Select Linq扩展方法返回格式化的超链接,并加入这些链接并分配给标签。

或者您可以在那里使用转发器绑定您的类别。

<强>更新:

foreach (DataListItem item in DataList4.Items)
{
    Repeater RepeaterQ = ((Repeater)(item.FindControl("Repeater1")));
    //string categories = ((Label)(item.FindControl("categoryLabel"))).ToString();
    string categories = ((Label)(item.FindControl("categoryLabel"))).Text;
    string[] arr1 = categories.Split(',');
    RepeaterQ.DataSource = arr1;
    RepeaterQ.DataBind();
}