开发者

GridView AllowPaging on asp.net

I have a gridview and I have enabled paging and added the event handler. Everything is working fine except I have to press each number twice for it to update.

Below is the code I am using for the initial load and the the call for the pagechanging event handler

protected void Page_Load(object sender, EventArgs e)
        {

            query = "SELECT log_id, use_username AS `Username`, log_type AS `Type`, "
                + "log_description AS `Description`, DATE_FORMAT(log_date, '%d-%m-%Y') AS `Date Created`, "
                + "DATE_FORMAT(log_time, '%H:%i') AS `Time Created` FROM log l, users u "
                + "WHERE l.log_userID = u.use_id";
            new DatabaseWork().loadGrid(query, tblLog);
            if (tblLog.Rows.Count == 0)
            {
                lblNoData.Visible = true;
            }

        }

protected void tblLog_PageChanging(object sender, GridViewPageEventArgs e)
        {
            new DatabaseWork().loadGrid(query, tblLog);
            tblLog.PageIndex = e.NewPageIndex;
        }

Below is the code being used to bind the data to the datagird which is being called from both the methods above

public void loadGrid(string query, GridView tblGrid)
        {
            using (DatabaseWork db = new DatabaseWork())
            {
                using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
                {
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {

                        DataSet ds = n开发者_StackOverflow社区ew DataSet();
                        da.Fill(ds);
                        tblGrid.DataSource = ds.Tables[0];
                        tblGrid.DataBind();
                    }

                }
            }

Everything is working fine except that each page number has to be pressed twice and I can't understand why.

Thanks for any help.


you need to put this page postback condition before your query If(!Page.IspostBack)

protected void Page_Load(object sender, EventArgs e)
    {
      If(!Page.IspostBack)
      {
        query = "SELECT log_id, use_username AS `Username`, log_type AS `Type`, "
            + "log_description AS `Description`, DATE_FORMAT(log_date, '%d-%m-%Y') AS `Date Created`, "
            + "DATE_FORMAT(log_time, '%H:%i') AS `Time Created` FROM log l, users u "
            + "WHERE l.log_userID = u.use_id";
        new DatabaseWork().loadGrid(query, tblLog);
        if (tblLog.Rows.Count == 0)
        {
            lblNoData.Visible = true;
        }
      }
    }

Edit:

protected void tblLog_PageChanging(object sender, GridViewPageEventArgs e)
    {
       query = "SELECT log_id, use_username AS `Username`, log_type AS `Type`, "
        + "log_description AS `Description`, DATE_FORMAT(log_date, '%d-%m-%Y') AS `Date Created`, "
        + "DATE_FORMAT(log_time, '%H:%i') AS `Time Created` FROM log l, users u "
        + "WHERE l.log_userID = u.use_id";

        new DatabaseWork().loadGrid(query, tblLog);
        tblLog.PageIndex = e.NewPageIndex;
    }


I guess the problem is that the Page_Load is executing before the GridView_PageChanging.

Try to load your data if the page is loaded for the first time using the IsPostBack property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜