Asp.Net SQL Refresh page duplicate inserts?

2020-04-21 08:45发布

I have a *.aspx page that contains a text box and a button. When the user enters information in to the textbox and clicks post, it inserts the data into my sql database. The issue is, that if the user hits refresh, it will keep inserting the same data into the database. I am pretty sure the whole "click" method is called, not just the insert call. I tried messing with sessions but it complains. I am not really sure what to do. I am looking for a simple easy fix.

protected void PostButton_Click(object sender, EventArgs e)
{
    string wpost = (string)Session["WallPost"];
    DateTime wtime = (DateTime)Session["WallDateTime"];
    if (txtWallPost.Text.Length > 0 && !wpost.Equals(txtWallPost.Text))
    {
        string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(strCon))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO [WallTable] ([UserId], [FriendId], [WallPost]) VALUES (@UserId, @FriendId, @WallPost)";
                cmd.Parameters.AddWithValue("@UserId", User.Identity.Name);
                cmd.Parameters.AddWithValue("@FriendId", User.Identity.Name);
                cmd.Parameters.AddWithValue("@WallPost", txtWallPost.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                Session.Add("WallPost", txtWallPost.Text);
                Session.Add("WallDateTime", new DateTime());
                conn.Close();
                txtWallPost.Text = "";
                LoadWallPosts();
            }
        }
    }
    return;
}

4条回答
够拽才男人
2楼-- · 2020-04-21 09:29

If the PostButton_Click is the server handler for the click event on your button then insert code shold be executed only if the user hits the post button - it should not happen in case of refresh.

I am pretty sure is not enough - to be sure put a breakpoint in your PostButton_Click method, hit F5 to run in debug mode and see if the breakpoint gets hit with a refresh.

If so it means that:

  1. you are calling explicitly PostButton_Click from somewhere else (Page_Load?)
  2. PostButton_Click was accidentally set as event handler for some other event on the page

A quick way of verifying hypotesis 1 & 2 is to run a search for PostButton_Click and see where you are calling it from or if it set as handler for some other element (maybe your form?) in the markup.

查看更多
叼着烟拽天下
3楼-- · 2020-04-21 09:37

It would be preferable to redirect the client to another page after the insert, however, if you really need the client stay in the same page, you can use a (bool) variable on your page to state if the insert has been done, and skip the insert logic if it has been executed before

查看更多
不美不萌又怎样
4楼-- · 2020-04-21 09:38

if he refreshes or press the back button the post will happen again, thus your page will process the same data thus insert once again.

You have to change your logic

查看更多
霸刀☆藐视天下
5楼-- · 2020-04-21 09:42

Add Response.Redirect(Request.Url.ToString(), false); to your button event and that should solve the problem.

查看更多
登录 后发表回答