0

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

protected void Page_Load(object sender, EventArgs e)
{
    // Auto refresh every 5 second
    Response.AppendHeader("Refresh",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 = GridView1.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 编辑模式下工作?

4

3 回答 3

0

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

于 2013-10-28T12:39:05.287 回答
0

谢谢大家,我已经通过使用计时器解决了这个问题。它就像一个魅力我的代码:

  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]
于 2013-10-29T03:32:35.870 回答
0

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

if (Session["myFlag"] == null)
{
    doStuff...
}
于 2013-10-28T10:42:44.450 回答