Asp.Net SQL刷新页面重复插入? [英] Asp.Net SQL Refresh page duplicate inserts?

查看:142
本文介绍了Asp.Net SQL刷新页面重复插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个文本框和一个按钮* .aspx页面中。当用户在到文本框中输入信息和点击后,其将数据插入我的SQL数据库。这个问题是,如果用户点击刷新时,将保持相同的数据插入到数据库中。我是pretty确保整个点击的方法被调用,而不仅仅是插入电话。我试图搞乱的会话,但它抱怨。我真的不知道该怎么做。我要寻找一个简单容易解决。

 保护无效PostButton_Click(对象发件人,EventArgs的发送)
{
    字符串wpost =(字符串)会议[WallPost];
    日期时间wtime =(DateTime的)会议[WallDateTime];
    如果(txtWallPost.Text.Length大于0&放大器;&放大器;!wpost.Equals(txtWallPost.Text))
    {
        串STRCON = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[\"SocialSiteConnectionString\"].ConnectionString;
        使用(SqlConnection的康恩=新的SqlConnection(STRCON))
        {
            使用(CMD的SqlCommand = conn.CreateCommand())
            {
                cmd.CommandText =INSERT INTO [WallTable]([用户ID],[FriendId],[WallPost])VALUES(@UserId,@FriendId,@WallPost);
                cmd.Parameters.AddWithValue(@用户ID,User.Identity.Name);
                cmd.Parameters.AddWithValue(@ FriendIdUser.Identity.Name);
                cmd.Parameters.AddWithValue(@ WallPost,txtWallPost.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                Session.Add(WallPost,txtWallPost.Text);
                Session.Add(WallDateTime,新的DateTime());
                conn.Close();
                txtWallPost.Text =;
                LoadWallPosts();
            }
        }
    }
    返回;
}


解决方案

添加的Response.Redirect(Request.Url.ToString(),FALSE);你的按钮事件,并且应该解决的问题。

I have a *.aspx page that contains a text box and a button. When the user enters information in to the textbox and clicks post, it inserts the data into my sql database. The issue is, that if the user hits refresh, it will keep inserting the same data into the database. I am pretty sure the whole "click" method is called, not just the insert call. I tried messing with sessions but it complains. I am not really sure what to do. I am looking for a simple easy fix.

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;
}

解决方案

Add Response.Redirect(Request.Url.ToString(), false); to your button event and that should solve the problem.

这篇关于Asp.Net SQL刷新页面重复插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆