davidxc davidxc - 1 month ago 7
ASP.NET (C#) Question

.Net VB email form sending twice for some reason

My email contact form (for some unknown reason) sends twice, sometimes even three times although this is rare.

Here's my VB:

<% @Import Namespace="System.Web.Mail" %>

<script language="vb" runat="server">

Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

Dim objMM as New MailMessage()
objMM.To = "info@emailaddress.com"
objMM.From = txtEmail.Text
'objMM.Cc = "someone@someaddress.com"
'objMM.Bcc = "someone@someaddress.com"
objMM.BodyFormat = MailFormat.Text
objMM.Priority = MailPriority.Normal
objMM.Subject = "Scotts Holiday Village"
objMM.Body = "Hello. At " + DateTime.Now + " an initial enquiry " & _
"was made " & _
"by a: " & txtTitle.Text & " " & txtFName.Text & " " & txtLName.Text & "." & vbCrLf & vbCrLf & _
""& txtAdOne.Text & vbCrLf & _
txtAdTwo.Text & vbCrLf & _
txtAdThree.Text & vbCrLf & _
""& txtCity.Text & vbCrLf & _
""& txtPostCode.Text & vbCrLf & vbCrLf & _
"Phone number: " & vbCrLf & txtPhone.Text & vbCrLf & vbCrLf & _
"Email address: " & vbCrLf & txtEmail.Text & vbCrLf & vbCrLf & _
"When are you looking to buy?: " & vbCrLf & txtWhenBuy.Text & vbCrLf & vbCrLf & _
"Estimated budget: " & vbCrLf & txtEstBudget.Text & vbCrLf & vbCrLf & _
"What's given you the idea of buying?: " & vbCrLf &txtIdea.Text & vbCrLf & vbCrLf & _
"Message: " & vbCrLf &txtMsg.Text & vbCrLf

SmtpMail.SmtpServer = "smtp.1and1.com"
SmtpMail.Send(objMM)
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub

</script>


The panelMailSent redirect to a thank you, confirmation page may have something to do with it, I'm not sure:

<asp:panel id="panelMailSent" runat="server" Visible="False">

<% Response.Redirect("/thankyou/message-sent.aspx") %>

</asp:panel>


The submit button:

<asp:Button runat="server" ID="btnSendFeedback" Text="Send my form"
OnClick="btnSendFeedback_Click" />


The form method:

<form method="post" id="genericForm" action="default.aspx" name="installer" onsubmit="showHide(); return false;">


Many thanks.

Answer

At the bottom of your page (with the submit button on it), the following javascript might help. This will prevent double-clicks by disabling the control:

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(DoubleClickHandler);
    function DoubleClickHandler(sender, args) 
    { 
        var c = args.get_postBackElement(); 
        c.disabled = true; 
    }
</script>

There are lots of other approaches; checking on the server side is safest, but more complex to implement in asp.net. A different approach sometimes used is to not prevent the double submit, but rather store these email requests (in a db, for example), then a task would send all the emails every 5 minutes (say), removing duplicates at the point.