Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Display GridView cell value as Link

Posted on 2009-04-15
7
Medium Priority
?
1,814 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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