?
Solved

Display GridView cell value as Link

Posted on 2009-04-15
7
Medium Priority
?
1,809 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
burakiewicz earned 2000 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

766 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