I have to following html code:
Email: <a href="mailto:...?subject=subject"><asp:Label style="margin-right: 90px;" ID="EmailLabel"
CssClass="InfoData" runat="server" Text="E-mail"></asp:Label></a>
Email: ``
as seen in the tag, I have a mailto. I need to add an email at the mailto, but this email will vary, so I can't hard code it. I am getting it from the database, so I do have it already, but how will I add it in the html code as a variable?
All the examples found have emails hardcoded in the mailto:
Since you want to control the value server-side, then I suggest using a server control, like this:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="mailto:[email protected]"
Text="[email protected]">
</asp:HyperLink>
NavigateUrl
and Text
are the properties you will want to interact with in the code-behind, like this:
// Get values from database
string emailAddress = GetEmailFromDatabase();
string subject = GetSubjctFromDatabase();
// Set NavigateUrl to use email address and subject values from above
HyperLink1.NavigateUrl = "mailto:" + emailAddress + "?subject=" + subject;
// You can also set the text of the hyper link here or in the markup
HyperLink1.Text = "Send email to " + emailAddress;
Note: The markup has a
NavigateUrl
value set, but you can remove it from the markup or just leave it, as the code-behind will overwrite it. I was just showing that the property exists, not trying to confuse you with hardcoding a value.
Add this to your markup:
<a id="mailtoLink" href="" runat="server">email</a>
In your code behind:
mailtoLink.Attributes["href"] = "mailto:[email protected]";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With