如何使自动刷新页面停止在Gridview编辑模式下工作?

时间:2013-10-28 10:17:33

标签: c# asp.net

我在Default.aspx.cs中的自动刷新代码:

    protected void Page_Load(object sender, EventArgs e)
            {
              //Auto refesh every 5 second
                Response.AppendHeader("Refesh",5+"; URL=Default.aspx");

             // Auto update into database
            con = new MySqlConnection(conStr);
                con.Open();
                cmd = new MySql.Data.MySqlClient.MySqlCommand();
                cmd.Connection = con;
                con = new MySql.Data.MySqlClient.MySqlConnection(conStr);


                GridView1.DataSourceID = "Datacmd";
                //Get status of process on server of pid(cells[6])
                count = GridView1.Rows.Count;
                string server = "";
                string pid = "";
                string status = "";
                 string cout=GriedView1.Rows.count;
                for (int i = 0; i < count; i++)
                {
                    server = GridView1.Rows[i].Cells[1].Text;
                    switch (server)
                    {
                        //If server locahost
                        case "localhost":
                            pid = GridView1.Rows[i].Cells[6].Text;
                            status = ws.GetStatusProcess(pid);   //Ws is My webservice have a function GetStatusProcess(string pid)
                            string SQL = "UPDATE command SET status='" + status + "' WHERE id=" + int.Parse(GridView1.Rows[i].Cells[0].Text) + "";
                            cmd.CommandText = SQL;
                            cmd.ExecuteNonQuery();
                            con.Close();
                            GridView1.DataSourceID = "Datacmd";
                            break;
                       // case : etc...
                        default:
                            break;

               }
           }
       }

自动更新到数据库工作正常。 但是,当我单击Gridview1上的按钮编辑(ShowEditButton =“True”)时,我无法在Gridview1上编辑行。因为页面会自动刷新。 如何使自动刷新页面停止在Gridview编辑模式下工作?

3 个答案:

答案 0 :(得分:0)

一种可能性是使用会话变量来存储状态。 您可以在gridview的RowEditing事件中设置值,并在RowUpdated事件和RowCancelingEdit事件中重置它。 在您的Page_Load方法中,您可以使用以下内容:

if (Session["myFlag"] == null)
{
    doStuff...
}

答案 1 :(得分:0)

我建议使用定时器控件而不是标头刷新。在page_prerender事件启用计时器中,如果gridview编辑索引&gt; -1,则禁用计时器。将计时器值设置为5000,间隔为5秒。希望这很清楚。

答案 2 :(得分:0)

谢谢大家, 我用Timer来解决这个问题。它就像一个魅力 我的代码:

  protected void Timer1_Tick(object sender, EventArgs e)
        {
            con = new MySqlConnection(conStr);
            con.Open();
            cmd = new MySql.Data.MySqlClient.MySqlCommand();
            cmd.Connection = con;
            con = new MySql.Data.MySqlClient.MySqlConnection(conStr);

            //Auto refesh database
            GridView1.DataSourceID = "Datacmd";

            //Auto get status(cells[7]) of pid(cells[6])
            count = GridView1.Rows.Count;
            string server = "";
            string pid = "";
            string status = "";

            for (int i = 0; i < count; i++)
            {
                server = GridView1.Rows[i].Cells[1].Text;
                switch (server)
                {
                    //server locahost
                    case "localhost":
                        pid = GridView1.Rows[i].Cells[6].Text;
                        status = ws.GetStatusProcess(pid);
                        string SQL = "UPDATE command SET status='" + status + "' WHERE id=" + int.Parse(GridView1.Rows[i].Cells[0].Text) + "";
                        cmd.CommandText = SQL;
                        cmd.ExecuteNonQuery();
                        con.Close();
                        GridView1.DataSourceID = "Datacmd";
                        break;

                    default:
                        break;
                }
            }
        }

[http://www.codeproject.com/Questions/359958/how-to-use-timer-in-asp-net][1]