0

我有一个允许用户输入信息的 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;
}
4

2 回答 2

4
于 2009-04-13T15:17:23.367 回答
0

我最终只是为墙上的帖子文本添加了一个会话 ID。在 post 方法中,它检查以确保文本框文本与会话文本不同。刷新时它会清除会话,因此工作正常。

于 2009-04-13T15:20:39.893 回答