asp.net updatepanel裡gridview更新需要點滑鼠兩次問題解決

來源:互聯網
上載者:User

在一個UpdatePanel中放入一gridView,點擊分頁時卻需要點擊兩次才顯示正確內容,剛開始的時候以為是UpdatePanel問題,在網上搜尋得到了一些結論但沒能解決問題。通過fiddler監控http資訊能看到在第一次點擊時傳遞的資訊是正確的,如:

能夠傳遞正確的頁碼,但返回的頁面資料卻不正確。所以應該是代碼問題,因為表格式資料是通過自訂過程讀取,所以每次頁面提交時都需要重新擷取資料,Gridview不能通過viewstate屬性傳遞其資料來源的。

擷取資料的代碼如:

 private void GetData()
    {
        localhost.QueryCondition condition = null;
        if (txtSearchValue.Value != "")//為空白查詢全部
        {
            condition = new localhost.QueryCondition
            {
                FieldName = slctSearchFields.Value,
                FieldOper = slctCondition.Value,
                FieldValue = txtSearchValue.Value
            };
        }

        localhost.CominfoTemplateService1 service = new localhost.CominfoTemplateService1();
        gridInfo.DataSource = service.SearchComsByUserName(Context.User.Identity.Name, condition);
        gridInfo.DataBind();
    }
而在表格分頁代碼是這樣的。
protected void gridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GetData();
   gridInfo.PageIndex = e.NewPageIndex;
    }
跟蹤代碼發現在grid的databind後,就會觸發rowdata_bind事件,會填充資料,而此時表格的頁碼還沒有發生變化,調用兩個語句的順序為:
gridInfo.PageIndex = e.NewPageIndex;
        GetData();
問題解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.