我有一个* .aspx页面,其中包含一个文本框和一个按钮。当用户输入信息到文本框并点击文章时,它会将数据插入到我的sql数据库中。问题是,如果用户点击刷新,它将继续向数据库中插入相同的数据。我很确定整个“点击”方法被调用,而不仅仅是插入调用。我试着搞会议,但它抱怨。我不知道该怎么做。我正在寻找一个简单的解决方法。Asp.Net SQL刷新页面重复插入?
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;
}
可能重复[如何防止重复回发混淆我的业务层](http://stackoverflow.com/questions/481564/how-to-prevent-repeated-postbacks-from-confusing-my-business-layer ) – 2011-09-04 13:59:54