I have a webform which on one page has a submit button. This submit button displays a processing modal whilst the email is tried to be sent. If the email sends, the page redirects, if it fails an error message is displayed. All this works fine but I have found an issue where if the user was the hit the 'Esc' key, no email is sent and it doesn't fall into my catch. It appears to stop my email process completely.

How can I ensure that if the user clicks the Esc key (or possibly and other key) that the process still carries on. Bit of a stage one I know.

HTML

    <div class="form-group">
        <div class="col-xs-12">
            <div class="pull-right">
                <asp:LinkButton ID="pg3button" runat="server" OnClick="pg3button_Click" CssClass="btn btn-primary" OnClientclick="ShowPopup();"><span aria-hidden="true" class="glyphicon glyphicon-ok"></span> Send & complete</asp:LinkButton>
            </div>
        </div>
    </div>

    <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">
                        <asp:Label ID="lblModalTitle" runat="server" Text="">Processing</asp:Label>
                    </h4>
                </div>
                <div class="modal-body">
                    <asp:Label ID="lblModalBody" runat="server" Text="">
                        <p class="text-center">
                            <img src="../Content/Images/loading.gif" />
                        </p>
                    </asp:Label>
                </div>
            </div>
        </div>
    </div>

    <script>
        function ShowPopup()
        {
           $('#myModal').modal();
        }
    </script>

Code behind

protected void pg3button_Click(object sender, EventArgs e)
{            
    try
    {
        //Create the msg object to be sent
        MailMessage msg = new MailMessage();

        //Add your email address to the recipients
        msg.To.Add("test@test.co.uk");

        //Configure the address we are sending the mail from
        MailAddress address = new MailAddress("test@test.co.uk");
        msg.From = address;

        //Append their name in the beginning of the subject
        msg.Subject = "Enquiry";

        msg.Body = Label1.Text + " " + Session["pg1input"].ToString()
                    + Environment.NewLine.ToString() +
                    Label2.Text + " " + Session["pg1dd"].ToString()
                    + Environment.NewLine.ToString() +
                    Label3.Text + " " + Session["pg2"].ToString();

        //Configure an SmtpClient to send the mail.
        SmtpClient client = new SmtpClient("smtp.live.com", 587);
        client.EnableSsl = true; //only enable this if your provider requires it

        //Setup credentials to login to our sender email address ("UserName", "Password")
        NetworkCredential credentials = new NetworkCredential("test@test.co.uk", "Password10");
        client.Credentials = credentials;

        //Send the msg
        client.Send(msg);

        Response.Redirect("/Session/pg4.aspx");
    }
    catch
    {
        //If the message failed at some point, let the user know
        lblResult.Text = "<div class=\"form-group\">" + "<div class=\"col-xs-12\">" + "There was a problem sending your request. Please try again." + "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-xs-12\">" + "If the error persists, please contact us." + "</div>" + "</div>";
    }
}

Related posts

Recent Viewed