【发布时间】:2010-10-19 03:22:23
【问题描述】:
我有一个 *.aspx 页面,其中包含一个文本框和一个按钮。当用户在文本框中输入信息并单击发布时,它将数据插入到我的 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;
}
【问题讨论】: