行添加次数与页面刷新次数相同

时间:2016-05-26 09:56:50

标签: c# asp.net

我在按钮点击事件的数据库中插入一些内容,每次插入数据时都能正常工作。

问题是我只是在点击按钮后刷新了页面然后我注意到按钮后单击数据插入的时间与刷新页面的次数相同。

我怎么能阻止这个? 这是我的按钮代码:

 protected void btn_AddEdu_Click(object sender, EventArgs e)
    {
        hfTab.Value = "edu";
        if (ValidateAddEdu())
        {
            emp_edu.InsertEdu(Session["empcd"].ToString(), ddl_degree.SelectedValue.ToString(), txt_eduterms.Text, ddl_institute.SelectedValue.ToString(), txt_edupassyear.Text, txt_edugrade.Text, ddl_sponsor.SelectedValue.ToString());
            int imagefilelength = fileupload_edu.PostedFile.ContentLength;
            byte[] imgarray = new byte[imagefilelength];
            HttpPostedFile image = fileupload_edu.PostedFile;
            image.InputStream.Read(imgarray, 0, imagefilelength);
            edu_attach.InsertEduAttachment(Session["empcd"].ToString(),ddl_degree.SelectedValue.ToString(),imgarray);
            lbl_eduerr.Text = "Added";
            lbl_eduerr.ForeColor = System.Drawing.Color.Green;
            BindEduGrid();

        }
    }

1 个答案:

答案 0 :(得分:0)

在.cs页面中添加以下代码

 protected void Page_Load(object sender, EventArgs e)
 {
            if (!Page.IsPostBack)
            {
                Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
            }
 }


 protected void Page_PreRender(object sender, EventArgs e)
 {
        ViewState["CheckRefresh"] = Session["CheckRefresh"];      
 }


 protected void btn_AddEdu_Click(object sender, EventArgs e)
 {
   if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
    {
    hfTab.Value = "edu";
     if (ValidateAddEdu())
     {
        emp_edu.InsertEdu(Session["empcd"].ToString(), ddl_degree.SelectedValue.ToString(), txt_eduterms.Text, ddl_institute.SelectedValue.ToString(), txt_edupassyear.Text, txt_edugrade.Text, ddl_sponsor.SelectedValue.ToString());
        int imagefilelength = fileupload_edu.PostedFile.ContentLength;
        byte[] imgarray = new byte[imagefilelength];
        HttpPostedFile image = fileupload_edu.PostedFile;
        image.InputStream.Read(imgarray, 0, imagefilelength);
        edu_attach.InsertEduAttachment(Session["empcd"].ToString(),ddl_degree.SelectedValue.ToString(),imgarray);
        lbl_eduerr.Text = "Added";
       //Add this line
        Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
        lbl_eduerr.ForeColor = System.Drawing.Color.Green;
        BindEduGrid();

     }
    }
}