Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set mailto from codebehind (or front-end)

Tags:

html

c#

asp.net

I have to following html code:

Email:&nbsp;<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:

like image 640
user1960836 Avatar asked Nov 26 '13 16:11

user1960836


2 Answers

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.

like image 137
Karl Anderson Avatar answered Oct 04 '22 04:10

Karl Anderson


Add this to your markup:

<a id="mailtoLink" href="" runat="server">email</a>

In your code behind:

mailtoLink.Attributes["href"] = "mailto:[email protected]";
like image 35
hutchonoid Avatar answered Oct 04 '22 03:10

hutchonoid