Update button keeps updating on click of Refresh in IE

别等时光非礼了梦想. 提交于 2019-12-11 16:32:56

问题


I have a form wherein I populate content using stored procedure and I perform update actions on the content. Now my update button works fine. My problem is that each time I click on 'refresh' button in IE, the content gets updated and I don't want that to happen. I am new to .Net and all this ViewState stuff. Any help is appreciated..

Here is my code:

            public partial class _Default : System.Web.UI.Page
            {

         protected void Page_Load(object sender, EventArgs e)
         {


         }
        public void BindGridView()
   {
    string constring =     ConfigurationManager
           .ConnectionStrings["shaConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("spd_pc", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = txt_city.Text;

            con.Open();
            IDataReader idr = cmd.ExecuteReader();
            GridView1.DataSource = idr;
            GridView1.DataBind();
            idr.Close();
            con.Close();

        }
    }
}
protected void Button1_click(object sender, EventArgs e)
{
    BindGridView();
}
private void DeleteRecords(int id)
{
    string constring = ConfigurationManager.
      ConnectionStrings["shaConnectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(constring))
    {

        using (SqlCommand cmd = new SqlCommand("del_pc", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            conn.Open();
            cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;

            cmd.ExecuteNonQuery();

            conn.Close();
        }
    }

}




protected void ButtonDelete_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)



        CheckBox chkb = (CheckBox)row.FindControl("CheckBox1");

        if (chkb.Checked)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);


            if (!(id.Equals(System.DBNull.Value)))
            {
                DeleteRecords(id);
            }
        }
    }
    BindGridView();
}
protected void Button2_Click(object sender, EventArgs e)
{
    if (!IsCallback)
    {

        string active = "active";
        string inactive = "inactive";
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkb = (CheckBox)row.FindControl("CheckBox1");

            if (chkb.Checked)
            {
                int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
                string status = row.Cells[5].Text;

                if (!(id.Equals(System.DBNull.Value)))
                {
                    if ((String.Equals(active, status)))
                        UpdateRecords(id);
                    if ((String.Equals(inactive, status)))
                        UpdateRecords(id);
                }
            }
        }
    }
}
private void UpdateRecords(int id)
{
    string constring = ConfigurationManager.
           ConnectionStrings["shaConnectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(constring))
    {

        using (SqlCommand cmd = new SqlCommand("upd_pc", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            conn.Open();
            cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;

            cmd.ExecuteNonQuery();

            conn.Close();
        }
    }
    BindGridView();
          }
      }     

回答1:


yes, this will happen. the problem is you are refreshing the post event of the button click. the solution is to redirect back to page after the update is complete.

button_click(...)
{
   //save to db
   Response.Redirect(Request.Referrer);
}

or something like that.

Now if the user clicks refresh it will submit the GET request to load the page, rather the POST to issue the button click event.

If you search for Post Get Redirect (or something like that) you will find lots of articles on the topic. describing both the situation you encountered and why this solution works.



来源:https://stackoverflow.com/questions/11021459/update-button-keeps-updating-on-click-of-refresh-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!