Solved

Display GridView cell value as Link

Posted on 2009-04-15
7
1,788 Views
Last Modified: 2013-11-07
I have a webapp written in ASP.Net (C#) and on one of my pages I have a GridView.  I have values stored in a table of a SQL database that are bound to a column in my GridView - we can call the column Links.  The values stored in the database are in the HTML tag format for a simple link - eg:

<a href="http://www.40k.com" target="_new">www.40k.com

There may be more than one of these link tags strung together in succession.  When these values are bound to the Links column of my GridView I need them to render as links and not as plain text - how can I accomplish this?
0
Comment
Question by:pprater1
  • 4
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 24147917
on the row databound event try this for the link column.  What is probably happening is that it is encoding the text when it first binds so it is actually showing >  for the >.  If you decode/replace it should render the actual tag.  Replace 0 with the column you are using
e.Row.Cells[0].Text = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
or if that doesnt work then try something like this
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(">",">").Replace("<","<");
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 24147942
Like on my first post.  I had & gt; without the space between & and gt;  in the lower code replace the @ with the &.  Had to put it so you could see what to replace
on the row databound event try this for the link column.  What is probably happening is that it is encoding the text when it first binds so it is actually showing @gt;  for the >.  If you decode/replace it should render the actual tag.  Replace 0 with the column you are using
e.Row.Cells[0].Text = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
or if that doesnt work then try something like this
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("@gt;",">").Replace("@lt;","<");
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24147972
You can use htmlencode

if (e.Row.RowType == DataControlRowType.DataRow)
             {                
 e.Row.Cells[3].Text = Server.HtmlDecode(e.Row.Cells[3].Text);
              }
But curious
What is the need of this being stored as html tags.
Cos grid view itself has hyper link columns we can use.Here is one such post

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24256154.html?sfQueryTermInfo=1+10+c+gridview+hyperlink
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:pprater1
ID: 24147994
I know, this is an enterprise application and this is old data that I'm having to deal with.  This page is a rewrite for an existing ASP page (not .NET) and I believe they were putting these directly on the page instead of in a grid, which was rendering correctly in the past.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 24148082
yeah the corrent way would be to store the url and the text as fields in the db then you could just bind to a hyperlink fields Text and NavigateUrl.  But back in asp i dont think you had that kind of option.  Also, if you were to write this out to the page i think it might still work, but i know with a grid it encodes the data
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 24148099
if you can get them to parse the links into urls and text and stored in the db you could do it, you might even be able to parse it with a select statement, but im not a dba
0
 

Author Closing Comment

by:pprater1
ID: 31570436
The HttpUtility.HtmlDecode is what worked - I didn't have to do the Replace step.  Thanks!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question