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>
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>