Button click doesn't stop triggering

时间:2017-04-10 02:43:07

标签: c# html asp.net

I have a button in an ASP page that I click to save some data to an SQL server. It works as intended, except that any time the page reloads (when doing other things) it triggers the submission again of that button. The result is duplicate records in the database. I realize I could probably validate for duplicates, but my concern is that if the button is still triggering a click on each refresh that it's consuming resources for no reason.

On each refresh it says "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."

I have searched the rest of the codebehind, HTML, and javascript file for any references to button11 and there are none. I'm stumped why it keeps triggering.

The button's HTML:

<asp:Button ID="button11" runat="server" OnClick="Button11_Click" />

Codebehind:

    protected void Button11_Click(object sender, EventArgs e)
{
    var update = hfptstate.Value;
    string fname = fnamebox.Text;
    string lname = lnamebox.Text;
    string sex = sexbox.Text;
    string combname = fname + " " + lname;
    string init = null;
    string str = combname;
    str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(i => init += i[0]);
    init += "_";
    Random rnd = new Random();
    int id = rnd.Next(1, 10000);
    string initid = init + id;
    string phone = phonebox.Text;
    string color = colorbox.Text;
    string age = agebox.Text;
    string datetime = DateTime.Now.ToString("MMM dd yyyy h:mmtt");

    SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Public\\public website\\slDataBase.mdf;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    if (statuslabel.InnerText == "Adding New Record")
    {
        cmd = new SqlCommand("INSERT into precordTable VALUES ('" + fname + "','" + lname + "','" + combname + "', '" + initid + "' , '' , '" + sex + "', '" + phone + "', '" + color + "', '" + age + "', '" + datetime + "', '','')", con);
    }
    else if (statuslabel.InnerText == "Editing Existing Record")
    {
        cmd = new SqlCommand("update precordTable set fname='" + fname + "', lname='" + lname + "', name='" + combname + "' , sex='" + sex + "', phone='" + phone + "', color='" + color + "', age='" + age + "' where initid='" + update + "';", con);
    }
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    statuslabel.InnerText = "Adding New Record";
    fnamebox.Text = "";
    lnamebox.Text = "";
    sexbox.Text = "";
    phonebox.Text = "";
    colorbox.Text = "";
    agebox.Text = "";

}

Edit: I also know about bobby drop tables. It's not a concern for this specific implementation.

Edit 2: I forgot to mention that just clicking the refresh button is enough to add duplicate records, and it doesn't even give the form submission warning ("To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.")

2 个答案:

答案 0 :(得分:2)

这是默认的浏览器行为。 您发送到服务器的最后一个请求是按钮的POST请求。 刷新浏览器将重做上一个请求,这将调用按钮单击事件。

以下链接可帮助您解决问题:
https://www.codeproject.com/Tips/319955/How-to-prevent-Re-Post-action-caused-by-pressing-b

答案 1 :(得分:1)

自从我开始使用ASP.NET WebForms(你的代码是webforms,对吗?)项目以来,它一直是个热议,而且我不完全确定没有任何关于你的情况的信息会使尝试理解什么可能是错误的差异,但我似乎记得一些关于页面生命周期的东西,因为设置为会话变量数据的数据没有重置为默认值,或者稍后在页面中稍后的代码事件方法它一直存在的生命周期。您应该能够通过调试那些不同事件中的代码来检查它。如果它不是那个问题,它可能是页面生命周期中没有其他吸引人的东西,而不是如何预期它的会话。

一个很好的参考是直接来自源代码:https://msdn.microsoft.com/en-us/library/ms178472.aspx,我认为自WebForms早期以来该页面已经有了显着改进。 HTH