How to UrlEncode a hyperlink in a GridView?

  • Thread starter Thread starter Author
  • Start date Start date
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 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.
 
Back
Top