Asp.net 表格背面相关
我在asp.net和C中有一个带有提交按钮的表单。表单用于通过电子邮件向网站管理员提交评论。C代码如下 但面临一个问题。i、 e.在刷新页面时,由于回发,它会再次以电子邮件的形式发送评论。我怎样才能避免这种情况?这是代码Asp.net 表格背面相关,asp.net,c#-4.0,webforms,Asp.net,C# 4.0,Webforms,我在asp.net和C中有一个带有提交按钮的表单。表单用于通过电子邮件向网站管理员提交评论。C代码如下 但面临一个问题。i、 e.在刷新页面时,由于回发,它会再次以电子邮件的形式发送评论。我怎样才能避免这种情况?这是代码 protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) {
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string body = "";
//body = body + "<html><head></head><body>";
body = body + "Dear Balvignan Team,\r\n";
if (txtComment.Text != null)
{
body = body + "Comment: " + txtComment.Text;
}
if (SendEmail(txtEmail.Text.Trim(), "Comment", body, false) == true)
{
lblContactAcknowledge.Text = "Thank You For <br />Submitting comment.";
lblContactAcknowledge.Visible = true;
PnlTalkToUs.Visible = false;
}
else
{
lblContactAcknowledge.Visible = false;
PnlTalkToUs.Visible = true;
}
}
SendEmail是发送电子邮件的功能。在页面加载事件中
您可以使用来检查是回发还是页面刷新。您可以使用以下选项:
if (SendEmail(txtEmail.Text.Trim(), "Comment", body, false) == true)
{
Response.Redirect("contact.aspx?success=true");
}
else
{
Response.Redirect("contact.aspx");
}
页面加载
if (!Page.IsPostback)
{
if (Request.QueryString["success"] == "true" )
{
lblContactAcknowledge.Text = "Thank You For <br />Submitting comment.";
lblContactAcknowledge.Visible = true;
PnlTalkToUs.Visible = false;
}
else
{
lblContactAcknowledge.Visible = false;
PnlTalkToUs.Visible = true;
}
}
检查一下!isPostBack在页面加载中,就是这样。有没有其他没有查询字符串参数的解决方案?@hetalgala,我添加了另一个解决方案
if (!Page.IsPostback)
{
if (Request.QueryString["success"] == "true" )
{
lblContactAcknowledge.Text = "Thank You For <br />Submitting comment.";
lblContactAcknowledge.Visible = true;
PnlTalkToUs.Visible = false;
}
else
{
lblContactAcknowledge.Visible = false;
PnlTalkToUs.Visible = true;
}
}
public bool EmailSent
{
get
{
return ViewState["EmailSent"] != null ? (bool)ViewState["EmailSent"] : false;
}
set
{
ViewState["EmailSent"] = value;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
if (!EmailSent)
{
if (SendEmail(txtEmail.Text.Trim(), "Comment", body, false) == true)
{
...
EmailSent = true;
}
else
{
...
}
}
}