0

My goal is to have multiple Hyperlinks in one single gridview's cell separated by comma, and the only way I could think of is to generate the HTML code when querying the data.

I have a simple query that will create number of Hyperlinks based on the match

SELECT A.TOLineKey, 
    STUFF((SELECT ',' + '<a href="XXX.com/soinformation.aspx?SONumber=' + CAST(SO3.SalesOrderNumber AS VARCHAR) + '" >' + CAST(SO3.SalesOrderNumber AS VARCHAR) + '</a>'
    FROM Table1 T 
    LEFT JOIN Table2 SO3 ON SO3.SOKey=T.SOKey 
    WHERE T.TOLineKey=A.TOLineKey FOR XML PATH(''))1,1,'') AS SONumber
FROM Table1 A
GROUP BY A.TOLineKey

The query above will produce the following result for SONumber:

<a href="XXX.com/soinformation.aspx?SONumber=123456" >123456</a>

However, when I pass the data above to girdview, it actually display the HTML code as string. Also the HTML code is showing with character entities when I check the code in browser.

> &lt;a href=&quot;XXX.com/soinformation.aspx?SONumber=123456&quot;&gt;123456&lt;/a&gt;

How should I edit my query for it to produce the correct HTML code to display in broswer?

I apologize if there was another similar question but I could not find one. Thank you for your help in advance.

3:35PM PST had to update my question for incorrect info provided.

alroc
  • 27,574
  • 6
  • 51
  • 97
joseph.c
  • 146
  • 13
  • It's not the SQL, the gridview by default filters out the html, there is a setting to enable it. (I can't remember off the top of my head). – Chuck Jun 26 '17 at 22:39
  • It's not that the query should be edited. It's that you should be doing some sort of decode when you're binding the rows in asp. See: https://stackoverflow.com/questions/431840/how-to-render-decoded-html-in-a-i-e-a-br-in-gridview-cell for example – ZLK Jun 26 '17 at 22:40
  • One more sample: https://stackoverflow.com/questions/35319985/html-in-a-gridview – Chuck Jun 26 '17 at 22:42
  • Good to know! Thank you guys! – joseph.c Jun 26 '17 at 22:43

1 Answers1

0

By default any html in a gridview will be encoded so You need to set the column to HtmlEncode="False"

Additional solutions here: How to render decoded HTML in a (i.e. a <br>) in GridView cell

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41