How to UrlEncode a hyperlink in a GridView?

Discussion in 'ASP .Net' started by Author, Aug 6, 2008.

  1. Author

    Author Guest

    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.
     
    Author, Aug 6, 2008
    #1
    1. Advertising

  2. Author

    Author Guest

    On Aug 6, 11:49 am, Author <> wrote:
    > 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.
     
    Author, Aug 6, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    4
    Views:
    10,327
    mikeb
    Jan 22, 2004
  2. Dario Sala
    Replies:
    1
    Views:
    3,824
    Karl Seguin
    Nov 15, 2004
  3. davetichenor
    Replies:
    1
    Views:
    859
    Eliyahu Goldin
    Oct 30, 2006
  4. Keith G Hicks
    Replies:
    3
    Views:
    780
    Keith G Hicks
    Feb 18, 2008
  5. Ken
    Replies:
    1
    Views:
    396
    Alvin Bruney
    Aug 16, 2003
Loading...

Share This Page