C# 防止在页面刷新时插入重复记录

C# 防止在页面刷新时插入重复记录,c#,asp.net,C#,Asp.net,我搜索了很多。。但未能实施。 这是我的代码,我如何防止页面刷新时重复插入,,(我如何使用viewstate或任何其他方法…resposnse.redirect在这种情况下不是解决方案) 您的问题似乎表明在页面刷新时触发了按钮事件处理程序,这很奇怪。页面刷新会导致GET操作。但是,对服务器端数据的更改只能在POST请求中进行。按钮事件处理程序只应在单击按钮时调用。我无法从这段代码看出您的体系结构以何种方式违反了这些规定。您可以在页面首次加载时使用视图状态或会话来存储标志,然后在插入数据库时将标志值

我搜索了很多。。但未能实施。 这是我的代码,我如何防止页面刷新时重复插入,,(我如何使用viewstate或任何其他方法…resposnse.redirect在这种情况下不是解决方案)


您的问题似乎表明在页面刷新时触发了按钮事件处理程序,这很奇怪。页面刷新会导致
GET
操作。但是,对服务器端数据的更改只能在
POST
请求中进行。按钮事件处理程序只应在单击按钮时调用。我无法从这段代码看出您的体系结构以何种方式违反了这些规定。

您可以在页面首次加载时使用视图状态或会话来存储标志,然后在插入数据库时将标志值更改为true,从这里获取完整代码:


resposnse.redirect在这种情况下不是解决方案为什么?因为它移动到了第一个选项卡。。但我希望它留在当前的账单上。。im在同一页面上使用多个选项卡(选项卡由单选按钮控制)在
inserallote
方法中添加重复的检查逻辑。我还使用了。。受保护的无效btn_save_alotee_click(对象发送方,事件参数e){if(txt_alotee_name.Enabled==true){----代码---我在刷新页面时调试代码,它不会转到btn_save_click(),但记录会插入到db Duplicate中我在刷新页面时调试代码,它不会转到btn_save_click()但是记录被插入到db Duplicate中如果它没有进入这个函数,记录就不会从这个函数中插入(或者不是在那个时间点)。除了您发布的代码之外,还有其他代码插入吗?
protected void btn_save_click(object sender, EventArgs e)
    { 
            System.Data.DataTable dt_plotid = new System.Data.DataTable();
            dt_plotid = FghBAL.Admitting.GetMax_ByPlotID(txt_plot.Text);
            string plotid = dt_plotid.Rows[0]["ID"].ToString();
            if (FghBAL.Alotee.InserAllotee(txt_alotee_name.Text.ToUpper(), ddl_alotee_sw.SelectedValue.ToUpper(), txt_alotee_fname.Text.ToUpper(), alotee_cnic, txt_alotee_phone.Text, txt_alotee_cellno.Text, txt_alotee_address.Text, txt_alotee_email.Text, plotid))
            {
                lbl_error_3.Visible = true;
                lbl_error_3.Text = "Add Successfully";
                txt_alotee_name.Enabled = false;
                ddl_alotee_sw.Enabled = false;
                txt_alotee_fname.Enabled = false;
                txt_alotee_cnic_1.Enabled = false;
                txt_alotee_cnic_2.Enabled = false;
                txt_alotee_cnic_3.Enabled = false;
                txt_alotee_phone.Enabled = false;
                txt_alotee_cellno.Enabled = false;
                txt_alotee_address.Enabled = false;
                txt_alotee_email.Enabled = false;
                System.Data.DataTable dt_alotee_data = new System.Data.DataTable();
                dt_alotee_data = FghBAL.Alotee.GetDatabyPlot_Id(plotid);
                grid_alotee.DataSource = dt_alotee_data;
                grid_alotee.DataBind();
                grid_alotee.Visible = true;
                btn_save_alotee.Visible = true;

                btn_link_another_alotee.Visible = true;
                lbl_grd_alotee.Visible = true;
                tabcontent3.Style.Add("height", "80%");
                //ViewState["dt_alotee_view"] = dt_alotee_data;
                btn_save_alotee.Enabled = false;
               // Response.Redirect(Request.Url.AbsoluteUri);
            }
            else
            {
                lbl_error_3.Text = "Unable to add Allottee Information";
            }
        }