我有一个asp:文本框,允许用户输入信息。当用户单击asp:按钮时,它会将文本框中的文本插入到我的sql数据库中,然后重定向回页面。如果用户点击刷新,则再次调用该按钮中的事件。我需要纠正这个问题,以便它不再被调用或者不会重新发布信息。
protected void PostButton_Click(object sender, EventArgs e)
{
if (txtWallPost.Text.Length > 0)
{
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();
conn.Close();
txtWallPost.Text = "";
LoadWallPosts();
Response.Redirect("~/UserPages/UserProfile.aspx?view="+"wall");
}
}
}
return;
}
答案 0 :(得分:4)
经典问题,带有经典解决方案:重定向后发布。您只需要在处理POST后将用户重定向到另一个页面(或同一页面......) - 当它们点击刷新时,最后一个请求(以及要重复的请求)将是GET。
答案 1 :(得分:0)
我最后只为墙贴文字添加了会话ID。在post方法中,它检查以确保文本框文本与会话文本的文本不同。在刷新时,它会清除会话,因此工作正常。