我有一個* .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