I was reading my site refferers today when I saw an interesting refferer query that came from Google. Three people got to my site after typing this phrase "hyperlink databind gridview mailto". I went to the the Google results page and immediately went to the page the was indexed by Google and upon futher review i found that my page wasn't really the answer to those persons question. Out of my curiousity i fired Visual Studio 2005 up and tried to add mailto link to a GridView using the HyperlinkField as my base.
<asp:HyperLinkField DataNavigateUrlFields="Email" DataNavigateUrlFormatString="mailto:{0}" DataTextField="Email" Text="Email me!" />
I ran the project thinking that this should work because this always works for me when i do same process for URLs. To my surprise, it didn't. For some unknown reason the column appears as plain text without any href attribute in sight. I went back to the HTML and checked it twice to see if maybe i missed something on the control attributes... hmmm, eveything looks ok. After a few minutes of playing around I resorted to the technique that never fails.. I converted the column to a TemplateField.
<asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:HyperLink ID="emailHyperLink" runat="server" NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' Text='<%# Eval("Email") %>' /> </ItemTemplate></asp:TemplateField>
Run the project again and sure enough, it worked. Huh! Weird! Anybody who knows what happened?
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.