Solved

Display GridView cell value as Link

Posted on 2009-04-15
7
1,775 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
Vb.net dynamic formulas in runtime 11 61
Split in Javascript 5 30
IIS Authorization for Web Service 2 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now