我在ASP頁面中有一個按鈕,點擊它可以將某些數據保存到SQL服務器。它按預期工作,除了在頁面重新加載時(當做其他事情時)它會再次觸發該按鈕的提交。結果是數據庫中的重複記錄。我意識到我可以驗證重複,但我擔心的是,如果按鈕仍然觸發每次刷新的點擊,那麼它會無故耗費資源。按鈕點擊不停止觸發
每次刷新時都會顯示「要顯示此頁面,Firefox必須發送信息,以便重複之前執行的任何操作(例如搜索或訂單確認)。」
我已經搜索了其餘的代碼隱藏,HTML和JavaScript文件的任何引用button11,沒有。我很難過,爲什麼它一直在觸發。
按鈕的HTML:
<asp:Button ID="button11" runat="server" OnClick="Button11_Click" />
代碼隱藏:
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 = "";
}
編輯:我也知道鮑比降表。這不是特定實施的關注點。
編輯2:我忘記提及,只需單擊刷新按鈕就足以添加重複記錄,甚至不會提供表單提交警告(「要顯示此頁面,Firefox必須發送將重複(例如搜索或訂單確認)「)
這聽起來像你不知何故引發Page_Load事件按鈕單擊事件。你可以發佈你在該事件中的任何代碼嗎? –
我評論了一切,它仍然發生。 – anesthetic