我有一个允许用户输入信息的 asp:textbox。当用户单击 asp:button 时,它将文本框中的文本插入到我的 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;
}