Email resent on page refresh

M

Morris Neuman

Hi,
My page has several textbox fields and a submit button. I have attempted to
reset the textbox fields to nulls after the email is sent. However, when the
user clicks on the page refresh button, though the textbox fields are empty,
another email gets sent (the email contains the fields previously entered in
the fields.

How do I set the fields to null and not have the email resent on page refresh?

protected void SubmitContactForm_Button1_Click(object sender, EventArgs e)
{
try
{

string RemoteHost = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (RemoteHost == "")
{
RemoteHost = Request.UserHostAddress;
}
string EmailSubject =
ConfigurationManager.AppSettings["ContactMsgSubject"];
string EmailFrom =
ConfigurationManager.AppSettings["WebMasterEmail"];
string EmailTo = ConfigurationManager.AppSettings["SalesEmail"];
string EmailCC = "";
string EmailBody = "Name=" + TextBox1.Text + " Phone=" +
TextBox2.Text + " Message=" + TextBox3.Text + " Remote Host=" + RemoteHost;

MailMessage message = new MailMessage();

message.From = new MailAddress(EmailFrom);

foreach (string torec in EmailTo.Split(';'))
{
if (torec != "")
{
message.To.Add((torec));
//message.To.Add(new MailAddress(rec));
}
}

message.Subject = EmailSubject;
message.Body = EmailBody;
message.IsBodyHtml = true;

SmtpClient client = new SmtpClient();

client.Send(message);

//Response.Redirect("index.aspx");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
ClientScript.RegisterStartupScript(GetType(), "OK!",
String.Format("alert('Email has successfully been sent to {0} and copied to
{1}');", EmailTo.Replace("'", "\'"), EmailCC.Replace("'", "\'")), true);
}
catch (System.Configuration.Provider.ProviderException c)
{
ClientScript.RegisterStartupScript(GetType(), "Error!",
String.Format("alert('Email has NOT been sent to {0} and copied to {1}');",
c.Message.Replace("'", "\'")), true);
}
}

protected void ResetContactForm_Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}



<h5>Contact form</h5>
<asp:Label id="Label1" runat="server" Text="Name"></asp:Label>

<asp:TextBox id="TextBox1" runat="server" Width="175px"></asp:TextBox>
<asp:Label id="Label2" runat="server" Text="Phone"></asp:Label>

<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Label id="Label3" runat="server" Text="Message"></asp:Label>

<asp:TextBox id="TextBox3" runat="server" Height="62px"
TextMode="MultiLine" Width="318px"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Submit"
onclick="SubmitContactForm_Button1_Click"/>

<asp:Button ID="Button2" runat="server" Text="Reset"
onclick="ResetContactForm_Button2_Click"/>
<p> </p>
 
A

Allen Chen [MSFT]

Hi Morris,
My page has several textbox fields and a submit button. I have attempted to
reset the textbox fields to nulls after the email is sent. However, when the
user clicks on the page refresh button, though the textbox fields are empty,
another email gets sent (the email contains the fields previously entered in
the fields.
How do I set the fields to null and not have the email resent on page
refresh?

From your description a user clicks refresh button on the browser and then
clicks the retry button on the popup window right? To avoid the code in the
button click event handler to be executed again in this case, the easiest
way is to add the following code at the bottom of the button click event
handler to redirect user to the same page. This will cause a get request
got sent when user clicking the refresh button, in which case the click
event handler will not be executed again.

Response.Redirect("YourPage.aspx"); //add it to the end of
SubmitContactForm_Button1_Click. Remember to replace YourPage.aspx with
your page name.

Please have a try and let me know if it can meet your requirement.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Morris Neuman

OK. So if I redirect then I do not get duplicate email send. However I do
not see the thank you message either. I tried the redirect after the
clientscript however that does not show the thank you message either. I
would like to show the thank you message and then redirect to a page. How
can I do that?

Response.Redirect("aboutSS-contacts.aspx");
ClientScript.RegisterStartupScript(GetType(), "OK!",
String.Format("alert('Thank you. Your request has been submitted to {0}');",
EmailTo.Replace("'", "\'")), true);
 
A

Allen Chen [MSFT]

Hi Morris,
OK. So if I redirect then I do not get duplicate email send. However I do
not see the thank you message either. I tried the redirect after the
clientscript however that does not show the thank you message either. I
would like to show the thank you message and then redirect to a page. How
can I do that?

In this case you can redirect user to another page to call the JavaScript
function to show thanks and redirect user back to the original page after
the page is loaded at client side.


Please try following code to test:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication21._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Click Me" />

</div>
</form>
</body>
</html>

Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
string EmailTo = "allen chen";
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
//Send email.
//When completed, redirect user to page2. Pass information via querystring

Response.Redirect(string.Format("Page2.aspx?showthanks=1&emailto={0}",EmailT
o));

}
}

Page2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs"
Inherits="WebApplication21.Page2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
setTimeout("Redirect()", 10);
function Redirect() {
window.location = "Default.aspx";
}
</script>
</div>
</form>
</body>
</html>

Page2.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
//Get information passed from original page.

string EmailTo = Request.QueryString["emailto"];
if (!String.IsNullOrEmpty(Request.QueryString["showthanks"])
&&
!String.IsNullOrEmpty(Request.QueryString["emailto"])
)
{
//Register JavaScript to show thanks. The redirect JavaScript is in
//Page2.aspx

ClientScript.RegisterStartupScript(GetType(), "OK!",
String.Format("alert('Thank you. Your request has been submitted to
{0}');",
EmailTo.Replace("'", "\'")), true);


}
}


Regards,
Allen Chen
Microsoft Online Support
 
M

Morris Neuman

Thanks Allen. Will try later. For now I am using your initial suggestion to
redirect to another page view where I will post the thankyou message.
--
Thanks
Morris


Allen Chen said:
Hi Morris,
OK. So if I redirect then I do not get duplicate email send. However I do
not see the thank you message either. I tried the redirect after the
clientscript however that does not show the thank you message either. I
would like to show the thank you message and then redirect to a page. How
can I do that?

In this case you can redirect user to another page to call the JavaScript
function to show thanks and redirect user back to the original page after
the page is loaded at client side.


Please try following code to test:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication21._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Click Me" />

</div>
</form>
</body>
</html>

Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
string EmailTo = "allen chen";
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
//Send email.
//When completed, redirect user to page2. Pass information via querystring

Response.Redirect(string.Format("Page2.aspx?showthanks=1&emailto={0}",EmailT
o));

}
}

Page2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs"
Inherits="WebApplication21.Page2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
setTimeout("Redirect()", 10);
function Redirect() {
window.location = "Default.aspx";
}
</script>
</div>
</form>
</body>
</html>

Page2.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
//Get information passed from original page.

string EmailTo = Request.QueryString["emailto"];
if (!String.IsNullOrEmpty(Request.QueryString["showthanks"])
&&
!String.IsNullOrEmpty(Request.QueryString["emailto"])
)
{
//Register JavaScript to show thanks. The redirect JavaScript is in
//Page2.aspx

ClientScript.RegisterStartupScript(GetType(), "OK!",
String.Format("alert('Thank you. Your request has been submitted to
{0}');",
EmailTo.Replace("'", "\'")), true);


}
}


Regards,
Allen Chen
Microsoft Online Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top