我的页面上有2个文本框和一个按钮。当我点击按钮时,文本框中的文本将通过电子邮件发送。但是,当我刷新页面并且没有内容时,我也会收到一封电子邮件。
protected void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(TextBox2.Text))
{
//email logic
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
//do nothing
}
}
在这里,点击按钮后,我收到一封电子邮件,但是当我刷新页面时,即使没有数据,它也会进入循环内部并收到一封电子邮件。
我如何阻止这种情况?
答案 0 :(得分:1)
在Page_Load
事件中执行以下操作,并将文本框和按钮保留在<asp:UpdatePanel>
中。然后每次刷新页面时页面都不会要求重新提交。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
}
<强>更新强>
将控件保持在UpdatePanel中,如下所示
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Send mail" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
答案 1 :(得分:0)
您需要使用POST&gt;重定向&gt; GET模式。这是解释link