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

时间:2014-10-26 14:34:39

标签: c# asp.net

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

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

2 个答案:

答案 0 :(得分:1)

您的问题似乎表明在页面刷新时触发了按钮事件处理程序 - 这很奇怪。页面刷新会导致GET操作。服务器端数据的更改应该只在POST请求中完成。只有在单击按钮时才应调用按钮事件处理程序。我无法从此代码中了解您的架构违反这些规定的方式。

答案 1 :(得分:0)

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

http://dotnetready4u.blogspot.com/2015/01/avoid-repaeted-insertion-on-page-refresh.html