How to UrlEncode a hyperlink in a GridView?

A

Author

I got into trouble with this problem.

The data I am presenting in a GridView control has a column called
"Website". That data comes from a stored procedure in SQL Server
2000. Most of them have valid http urls. But some of them say
"None" or "Not available".

In the GridView, I would like create a hyperlink for those valid http
urls only, and simply plain-output those which say "None" or "Not
available".

I tried the HyperLinkField of Gridview, but I don't think we can
conditionally turn off a hyperlink through this field, correct?

So, instead, I managed to create hyperlinks for those valid http urls
in the stored procedure by doing this:

CASE WHEN CHARINDEX('http', Website) THEN
'<a href="'> + Website + ' ">' + Website + '</a>'
ELSE
Website
END AS Website

The output looks perfect in SQL Sever Management Studio.

In the GridView, I simply bind this Website field to a BoundField.
But, GridView does not render the hyper link for me, instead it simply
output the hyperlink at a HTML text like so:

<a href="http://www.google.com">http://www.google.com</a>

How can I let GridView render the hyperlink for me? I was trying to
do this:

<asp:BoundField HeaderText="Website" DataField='<%
Server.UrlEncode("Website") %>' />

But it isn't successful.

Any idea? Thanks.
 
A

Author

I got into trouble with this problem.

The data I am presenting in a GridView control has a column called
"Website".  That data comes from a stored procedure in SQL Server
2000.  Most of them have  valid http urls.  But some of them say
"None" or "Not available".

In the GridView, I would like create a hyperlink for those valid http
urls only, and simply plain-output those which say "None" or "Not
available".

I tried the HyperLinkField of Gridview, but I don't think we can
conditionally turn off a hyperlink through this field, correct?

So, instead, I managed to create hyperlinks for those valid http urls
in the stored procedure by doing this:

CASE WHEN CHARINDEX('http', Website) THEN
     '<a href="'> + Website + ' ">' + Website + '</a>'
ELSE
     Website
END AS Website

The output looks perfect in SQL Sever Management Studio.

In the GridView, I simply bind this Website field to a BoundField.
But, GridView does not render the hyper link for me, instead it simply
output the hyperlink at a HTML text like so:

<a href="http://www.google.com">http://www.google.com</a>

How can I let GridView render the hyperlink for me?  I was trying to
do this:

<asp:BoundField HeaderText="Website" DataField='<%
Server.UrlEncode("Website") %>' />

But it isn't successful.

Any idea?  Thanks.

I just recall that there is a property called HtmlEncode of
BoundField. Set it to false, and it works. Thanks for reading anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top