4

关于我的这个问题,

GridView 表 1 相关表 2

我有一个现在看起来像这样的gridview,

在此处输入图像描述

这是SQL小提琴

问题:

如何使用 FruitTitle 创建 HyperLinkField 并链接到水果网站?

这是我Types_of_Fruits_in_Crate目前用于显示的代码,并且运行良好,

            BoundField theField = new BoundField();
            theField.DataField = "Types_of_Fruits_in_Crate";
            gv.Columns.Add(theField);

放什么

            HyperLinkField theField = new HyperLinkField();
            theField.DataTextField = 'Types_of_Fruits_in_Crate';
            theField.DataNavigateUrlFields = // not sure ....
4

2 回答 2

2

使用文字控件而不是超链接,然后尝试使用以下查询:

SELECT CrateTitle,CrateDescription,CrateID,
stuff(
(
  SELECT '<a href=''' + [FruitWebsite] + ''' target=''_blank''>'+ [FruitTitle] +'</a>'
  FROM fruits WHERE CrateID = t.CrateID FOR XML path('')
),1,1,' ') Types_of_Fruits_in_Crate
FROM (SELECT DISTINCT CrateTitle,CrateDescription,CrateID FROM fruits )t
于 2013-04-25T16:37:02.617 回答
2

试试下面的方法,它会帮助你...

SQL FIDDLE: http ://www.sqlfiddle.com/#!3/96a49/18

SQL:

SELECT CrateTitle,CrateDescription,CrateID,
stuff(
(
    SELECT ',<a href=''' + [FruitWebsite] + ''' target=''_blank''>'+ [FruitTitle] +'</a>' FROM fruits WHERE CrateID = t.CrateID FOR XML path('')
),1,1,' ') Types_of_Fruits_in_Crate
FROM (SELECT DISTINCT CrateTitle,CrateDescription,CrateID FROM fruits )t

此外,在您的代码中添加HTML ENCODE = FALSE然后只有 HTML 功能反映在您的网格视图中

C# :

    BoundField theField = new BoundField();
    theField.DataField = "Types_of_Fruits_in_Crate";
    theField.HtmlEncode = false;
    gv.Columns.Add(theField);
于 2013-04-25T16:48:41.370 回答