Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

c# - How to create a HyperLink field out of SQL query

In reference to this question of mine,

GridView Table 1 related to Table 2

I got a gridview which looks like this at the moment,

enter image description here

Here is the SQL fiddle

Question:

How Can I create a HyperLinkField with FruitTitle and link to fruit website ?

This is the code I am using for displaying Types_of_Fruits_in_Crate at the moment and works perfectly,

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

what to put in

            HyperLinkField theField = new HyperLinkField();
            theField.DataTextField = 'Types_of_Fruits_in_Crate';
            theField.DataNavigateUrlFields = // not sure ....
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try like below, it will help you...

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

Also, Add HTML ENCODE = FALSE in your Code then only the HTML functionality reflect in your Grid View

C# :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...