更新按钮不断更新的IE浏览器中刷新的点击 [英] Update button keeps updating on click of Refresh in IE

查看:137
本文介绍了更新按钮不断更新的IE浏览器中刷新的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中使用我的存储过程填充的内容和我进行的内容更新的动作。现在我的更新按钮正常工作。我的问题是,每次我点击IE浏览器中的刷新按钮,内容得到更新,我不希望这样的事情发生。我是新来的.Net而这一切的ViewState的东西。任何帮助是AP preciated ..

下面是我的code:

 公共部分类_Default:System.Web.UI.Page
            {         保护无效的Page_Load(对象发件人,EventArgs的发送)
         {
         }
        公共无效BindGridView()
   {
    字符串constring = ConfigurationManager中
           .ConnectionStrings [shaConnectionString]的ConnectionString。    使用(SqlConnection的CON =新的SqlConnection(constring))
    {
        使用(CMD的SqlCommand =新的SqlCommand(spd_pcCON))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = CON;
            cmd.Parameters.Add(@城,SqlDbType.VarChar).value的= txt_city.Text;            con.Open();
            IDataReader的IDR = cmd.ExecuteReader();
            GridView1.DataSource = IDR;
            GridView1.DataBind();
            idr.Close();
            con.Close();        }
    }
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    BindGridView();
}
私人无效DeleteRecords(INT ID)
{
    字符串constring = ConfigurationManager中。
      的ConnectionStrings [shaConnectionString]的ConnectionString。    使用(SqlConnection的康恩=新的SqlConnection(constring))
    {        使用(CMD的SqlCommand =新的SqlCommand(del_pc,康涅狄格州))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection =康恩;
            conn.Open();
            cmd.Parameters.Add(@ ID,SqlDbType.VarChar).value的= ID;            cmd.ExecuteNonQuery();            conn.Close();
        }
    }}
保护无效ButtonDelete_Click(对象发件人,EventArgs的发送)
{
    的foreach(在GridView1.Rows GridViewRow行)        复选框chkb =(复选框)row.FindControl(CheckBox1);        如果(chkb.Checked)
        {
            INT ID = Convert.ToInt32(GridView1.DataKeys [row.RowIndex] .Values​​ [0]);
            如果(!(id.Equals(System.DBNull.Value)))
            {
                DeleteRecords(ID);
            }
        }
    }
    BindGridView();
}
保护无效Button2_Click(对象发件人,EventArgs的发送)
{
    如果(!IsCallback)
    {        串活性=主动;
        字符串无效=无效;
        的foreach(在GridView1.Rows GridViewRow行)
        {
            复选框chkb =(复选框)row.FindControl(CheckBox1);            如果(chkb.Checked)
            {
                INT ID = Convert.ToInt32(GridView1.DataKeys [row.RowIndex] .Values​​ [0]);
                字符串状态= row.Cells [5]。文本;                如果(!(id.Equals(System.DBNull.Value)))
                {
                    如果((String.Equals(活动,状态)))
                        UpdateRecords(ID);
                    如果((String.Equals(无效,状态)))
                        UpdateRecords(ID);
                }
            }
        }
    }
}
私人无效UpdateRecords(INT ID)
{
    字符串constring = ConfigurationManager中。
           的ConnectionStrings [shaConnectionString]的ConnectionString。    使用(SqlConnection的康恩=新的SqlConnection(constring))
    {        使用(CMD的SqlCommand =新的SqlCommand(upd_pc,康涅狄格州))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection =康恩;
            conn.Open();
            cmd.Parameters.Add(@ ID,SqlDbType.VarChar).value的= ID;            cmd.ExecuteNonQuery();            conn.Close();
        }
    }
    BindGridView();
          }
      }


解决方案

是的,这会发生。问题是你的刷新按钮点击的事件后。解决的办法是回重定向到网页更新完成后。

  button_click(...)
{
   //保存到数据库
   的Response.Redirect(Request.Referrer);
}

或类似的东西。

现在,如果用户点击刷新,将提交GET请求来加载页面,而该邮政发行的按钮单击事件。

如果您搜索后获得重定向(或类似的东西),你会发现很多关于该主题的文章。描述两种情况下,你遇到过,为什么这个解决方案工作。

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

解决方案

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.

这篇关于更新按钮不断更新的IE浏览器中刷新的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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