Solved

Display GridView cell value as Link

Posted on 2009-04-15
7
1,766 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:pprater1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The HttpUtility.HtmlDecode is what worked - I didn't have to do the Replace step.  Thanks!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

10 Experts available now in Live!

Get 1:1 Help Now