Search code examples
javascripthtmlasp.netmailto

Programmatically instantiating a mailto


I have a website with a "place order" button, and above it is a list of products and textboxes allowing me to set a quantity for each product.

On the click of the button it posts back and calculated how many of each product is required, I then put this into a big string with "mailto:[email protected]" at the front. I now want to somehow get that to pop up into the client automatically at the end of the postback.

I've tried window.open but this also opens a new browser window which I want to avoid.

Button postback code:

protected void btnPlaceOrder_Click(object sender, EventArgs e)
        {
            string url = "mailto:[email protected]?subject=New order from " + ddlSelectLocation.SelectedItem.Value;
            url += "&body=Please raise a new order for the following items:" + Environment.NewLine;
            foreach (GridViewRow row in grdOrder.Rows)
            {
                string model = row.Cells[0].Text;
                int qty = 0;
                TextBox txt = (TextBox)row.Cells[3].Controls[1];
                if (int.TryParse(txt.Text, out qty))
                {
                    if (qty > 0)
                        url += " - " + model + ": " + qty.ToString() + Environment.NewLine;
                }
            }
            url += Environment.NewLine + "Many Thanks.";

            Response.Write(url);
        }

Otherwise I'm going to have to write a load of clientside javascript code :(


Solution

  • Can you try something like this?

        Page.ClientScript.RegisterStartupScript(this.GetType(),
            "MAIL",
            "window.open('" + url + "')",
            true);
    

    You'll have to make sure the string is properly escaped...