HyperlinkField doesn’t work in GridView control

In my previous project, I had to display e-mail addresses for each row in the GridView as a hyperlink to enable users to click on the link which will take them to compose e-mail in OutLook. I was taken aback to see that after the page was rendered, the link didn’t show but the e-mail address was shown as a static text. Bummer!!! I did some research on why ASP.NET 2.0 was behaving like that and found out that the src/href elements were disabled because the data might be malicious. But after few hours of trying, I found a workaround: Instead of using Hyperlink field, we can use a BoundField and set the DataFormatString property.

<a href=mailto:{0}>{0}</a> and this works perfectly. Here {0} is your datafield column from your datasource which has the e-mail address.

7 Responses to “HyperlinkField doesn’t work in GridView control”

  1. Vivek R S Says:

    Perfect! This is exactly what I was looking for.
    Thanks a lot Vasudevan
    -Vivek R S

  2. THANK YOU! I stand amazed in your glory!

  3. Jason Lowenthal Says:

    Very Clever, thanks for the idea!

  4. John Paul C Says:

    Thank you, it worked! However, I had to convert the field into a templatefield. Here is the code for those interested.

    <asp:Label ID=”Label1″ runat=”server” Text='<%# Bind(“email”, “{0}“) %>’>

    <asp:TextBox ID=”TextBox1″ runat=”server” Text=”>

  5. Thank You very much! Helped me alot. Great!

  6. And don’t forget to write for field HtmlEncode=”false” !

  7. Great Job..It worked but is it possible to populate the from field once anyone clicks on the hyperlink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: