Lucretius
Lucretius

Reputation: 1059

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.

I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.

These are full Hyperlinks such as "Http://stackoverflow.com/"

Originally the GridView column was a simple BoundField like this:

<asp:BoundField DataField="JobDescription" HeaderText="JobDescription" 
    SortExpression="JobDescription" />

So I started trying to convert it into a hyperlink field.

<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
    DataTextField="JobDescription" 
    HeaderText="JobDescription"
    SortExpression="JobDescription" 
    Target="_blank" 
    NavigateUrl="{0}" />

This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.

Either way will work...

I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.

I appreciate the help.

Upvotes: 0

Views: 1249

Answers (2)

Lucretius
Lucretius

Reputation: 1059

Grrr found the answer:

<asp:BoundField DataField="JobDescription" HeaderText="Job Description" 
    SortExpression="JobDescription" 
    DataFormatString="<a target='_blank' href='{0}'>Text</a>" 
    HtmlEncode="False" />

You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...

&nbsp;

The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

Upvotes: 0

Jude Cooray
Jude Cooray

Reputation: 19862

Use a Template Field. So your can define your normal view and editing view.

Upvotes: 1

Related Questions