Asp.net 每次刷新页面时插入记录
我有以下代码: 当我在数据库中插入一行时,它的插入是正确的,但当我刷新页面时,最后插入的行会一次又一次地插入Asp.net 每次刷新页面时插入记录,asp.net,Asp.net,我有以下代码: 当我在数据库中插入一行时,它的插入是正确的,但当我刷新页面时,最后插入的行会一次又一次地插入 protected void ButtonExecute_Click(object sender, EventArgs e) { string connectionString =cs.getConnection(); string insertSql = "INSERT INTO profitCategories(n
protected void ButtonExecute_Click(object sender, EventArgs e)
{
string connectionString =cs.getConnection();
string insertSql = "INSERT INTO profitCategories(name, IdUser) VALUES(@name, @UserId)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand command = new SqlCommand(insertSql, myConnection);
command.Parameters.AddWithValue("@name", TextBoxCategory.Text);
command.Parameters.AddWithValue("@UserId", cui.getCurrentId());
command.ExecuteNonQuery();
myConnection.Close();
}
TextBoxCategory.Text = string.Empty;
}
这解决了我的问题:在方法的末尾,执行重定向到同一页面,这将清除发布的数据 大概是这样的:
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
请参阅此链接。。这里也有同样的问题……这不起作用,如果重新加载页面正确,仍然可以复制上一条记录。我改变了答案。看看这对你是否有效。