Scott on Writing

Musings on technical writing...

Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!)

If you have an Email field in a database and want to display this email as a formatted email address - mailto:emailAddress -  in a GridView or DetailsView control in ASP.NET 2.0 you might initially reach for the ol' trusty HyperLinkField, setting its DataTextField property to EmailIDColumnName and its DataNavigateUrlFields and DataNavigateUrlFormatString properties to EmailIDColumnName and mailto:{0}, respectively. Doing so won't give you any warning or error message, but when you view the page in a browser you'll find that the email address is displayed, but not as a clickable email address. Boo.

According to ninatang, an ASP.NET Team Member:

This is due to a security change introduced after Beta2 for ImageField and HyperLinkField to remove the src/href if the data was potentially malicious. You can work around this change by using a TemplateField instead.
[Read nina's messageboard post...]

To remedy this, use a TemplateField, as nina suggests. The easiest way to do this is (IMO) is to add the HyperLinkField and configure its properties as I noted above through the Fields dialog box in the Design view (reached by choosing Edit Columns/Fields from the GridView/DetailsView's smart tag), and then click the “Convert this field into a TemplateField” link. Or, if you prefer to do this by hand in the Source view you can use:

<asp:TemplateField>
  <ItemTemplate>
    <asp:HyperLink runat=”server” Text='<%# Eval(“EmailIDColumnName“) %>' NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' />
  </ItemTemplate>
</asp:TemplateField>

posted on Friday, December 02, 2005 5:12 PM

Feedback

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 12/3/2005 3:10 AM AsbjornM

Yes, but.. you can't use EnableSortingAndPagingCallbacks if you use this.
According to this blog: http://dotnetjunkies.com/WebLog/saravana/archive/2005/01/07/42433.aspx
Code for handling ICallbackEventHandler.RaiseCallbackEvent will be generated when using EnableXX but that doesn't happen in RTM.
I'm trying to implement ICallbackEventHandler but those events don't get called.

So, from where I stand it looks like the gridview is kinda difficult to use when you want special formatting.

I'm trying to use RowDataBound event to reformat the data to be viewed, but I'm not there yet..

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 3/24/2006 3:17 AM Thany

Here goes another one of those rediculus Microsoft security measures. Isn't it up to the programmer what he wants to ouput to the client? Who does the framework think he is, declining me from putting some simple and harmless links on a DataGrid??

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 3/24/2006 8:33 AM Scott Mitchell

Thany, I see your gripe, but in the same vein if Microsoft didn't do this and it led to a security hole, people would blame MS. They are kind of between a rock and a hard place, needing to balance security concerns and ease of development. MS has been too far to the ease of development/usability (think ActiveX) in the past and is (seemingly) trying to right that, but they might be swinging too far toward the security side...

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 4/1/2006 12:53 AM VS

Hi,

That is a good one. How do I do the whole thing from code behind? I would like to add TemplateField and ItemTemplate and also add the HyperLink control (to display mailto:e-mailAddress) from code behind and not thru the designer.

A code snippet would be awesome.

Thanks.

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 4/1/2006 10:35 AM VS

Hi all,

Actually, I found a simple solution:

Instead of using a Hyperlink field, we can use a BoundField and set the DataFormatString of the BoundField to look like this:

<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.

I have answered my own question and made it even simpler.

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 11/7/2007 2:28 PM Theo

Thanks for the solution! The BoundField way is easy and works like a champ.

- Theo

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 12/19/2007 8:03 AM Armaghan

Thanks for the BoundField workaround.
I was banging my head on my desk for the longest time to figure this out without the use of TemplateFields, untill i came across this post..

# 
I can&#8217;t seem to get a HyperLinkField to display &amp; link to an email address, using either DataNavigateURLFormatString or even a separate field, to provide the &#8216;mailto:&#8217; part | it.rss24h.com 7/21/2009 9:17 PM Pingback/TrackBack


I can&#8217;t seem to get a HyperLinkField to display &amp; link to an email address, using either DataNavigateURLFormatString or even a separate field, to provide the &#8216;mailto:&#8217; part | it.rss24h.com

# re: Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!) 2/16/2010 10:42 PM Javier

You miss the HtmlEncodeFormatString="false" to make the DataFormatString works.

Cheers,

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles