Using Mailto in ASP:HyperLink

So I wanted to add a mailto link in my code. I knew that it would be something to do with the asp:HyperLink control but was not sure how to use it. After a little testing I found this information out.

So I had pulled my email address from a SQL statement and wanted to add it to the control, this was my first attempt:

<asp:HyperLink ID="hlEmail" NavigateUrl="mailto:" Text='<%# Bind("Email") %>' runat="server"></asp:HyperLink>

This gave me a HyperLink but with a blank mailto, well that was no good. So next I got to thinking that I need to stick a Bind or Eval with the text on the NavigateUrl section:

<asp:HyperLink ID="hlEmail" NavigateUrl='mailto:<%# Bind("Email") %>' Text='<%# Bind("Email") %>' runat="server"></asp:HyperLink>

This gave me a link, but it do not work. Every time I clicked on the link nothing happened. Again I tried some more code and came up with the following:

<asp:HyperLink ID="hlEmail" NavigateUrl='<%# Bind("Email", "mailto:{0}") %>' Text='<%# Bind("Email") %>' runat="server"></asp:HyperLink>

This worked, so it was to do with formatting the string.  By using the in built string formatting options I was able to pass the Bind vale to the NavigateUrl parameter. Easy when you know how.

Advertisements

3 Responses to Using Mailto in ASP:HyperLink

  1. Aw, this was a really good post. Spending some time and actual effort to create a very good article… but what can I say… I procrastinate a lot and never seem to get anything done.

  2. Matt Bell says:

    Thanks, glad that it helped.

  3. You ought to be a part of a contest for one of the most useful sites online.
    I will recommend this website!

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: