Does anyone know how can I set the asp:HyperLink href to “mailto:[email protected]” in .net c#?
Example:
If I have the following code:
<tr>
<td class="graytext r">PERSONAL EMAIL:</td>
<td><asp:HyperLink runat="server" ID="sPersonalEmail" class="orange" style="cursor:pointer" /></td>
</tr>
How can I set the href to “mailto:[email protected]” in .net c# instead of hard code it in asp:HyperLink?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Something like this by setting NavigateUrl:
<asp:HyperLink runat="server" NavigateUrl='<%# Bind("Email", "mailto:{0}") %>'
Text='<%# Bind("Email") %>'
ID="hlEmail">
</asp:HyperLink>
Method 2
I find this the easiest
string whateverEmail = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e591809691a5918d8c96cb868a88">[email protected]</a>";
hypEmail.Attributes.Add("href", "mailto:" + whateverEmail );
Method 3
If you wanted to do it code behind then you could simply put the following in page load (or wherever relevant, such as a button event):
string email = “[email protected]”;
sPersonalEmail.NavigateUrl = “mailto:” + email;
Method 4
Another way is this:
<asp:BoundField DataField="Email" DataFormatString="<a href=mailto:{0}>{0}</a>" HtmlEncodeFormatString="false" />
Method 5
This is my ASP.NET code using the asp:HyperLink properties.
hlEmail.Text = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691d010c2c04080005291e0c0b280d0d0c1a1a470a0604">[email protected]</a>"; hlEmail.NavigateUrl = "mailto:" + "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d1cdc0e0c8c4ccc9e5d2c0c7e4c1c1c0d6d68bc6cac8">[email protected]</a>";
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0