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

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

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles