Thando Tee
Thando Tee

Reputation: 121

Use a declared variable in the mailto hyperlink

I have a variable which is passed over from another C# class using the Request.QueryString[], now I want to take the value of that variable and use it in the mailto hyperlink. I don't want to hard code the email address.

The normal mailto link is:

<a href"mailto:[email protected]: />

what I want to do is take the email provided by the user from a previous page...

I have: Request.QueryString["newEmail"];

I tried this: <a href="mailto:Request.QueryString["newEmail"]" />

but it doesn't work, please help!!!!

Upvotes: 1

Views: 2047

Answers (2)

codeandcloud
codeandcloud

Reputation: 55248

Use asp:HyperLink which gets rendered as the anchor tag.

'Example:

<asp:HyperLink ID="EmailLink" runat="server"
        NavigateUrl='<% "mailto:" + Request.QueryString["newEmail"] %>'>
    Send Mail
</asp:HyperLink>

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128776

I have: RequestQueryString["newEmail"];

Should be Request.QueryString["newEmail"].

Your question doesn't show where you're passing this "newEmail" to the query string.

I tried this: <a href="mailto:RequestQueryString["newEmail"]" />

Unless that's a back-end string, you can't combine front-end and back-end code like this. You'd need to wrap it with <% %>, and even then you'd not do this because you'd need checks on whether the query string contains a value.

Back-end:

protected string email = "";

public string GetEmail() {
    return email;
}

protected virtual void Page_Load(object sender, EventArgs e) {
    if (Request.QueryString["newEmail"] != null)
        email = Request.QueryString["newEmail"];
}

Front-end:

<a href="mailto:<%# GetEmail() %>"></a>

Upvotes: 0

Related Questions