GridView中的分页不适用于异步调用 [英] paging in GridView does not work with asynchronous calls

查看:135
本文介绍了GridView中的分页不适用于异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



继续我的异步调用我在Windows窗体中使用调用DataGridView工作。



在ASP.NET中使用async dataload和databind时,我无法直接调用GridView组件。



运行下一个代码时:

Hello everyone,

Continuing my async calls I have it working in windows forms with Invoking the DataGridView.

When using async dataload and databind in ASP.NET I cannot invoke the GridView component directly.

When the next code is run:

public partial class WebForm1 : System.Web.UI.Page
    {
       
        int pageNumber = 0;
        delegate void updateGridViewDelegate(DataTable aTable, int pageNumber);
        string query = "select top 100 * from [dbo].[FactInternetSales]";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DBConnecting.DataObjects.DataObjects src = new DBConnecting.DataObjects.DataObjects();
                src.ReturnDataTable += new DBConnecting.EventHandlers.DataTableEventArgs.DataTableEventHandler(HandleDataTable);
                src.AsyncDataTableStart(query, CommandType.Text);
            }
        }

        private void HandleDataTable(object sender, DataTableEventArgs e)
        {
           
            DataTable table = e.DataTableInfo;
            updateGridViewDelegate updDel = new updateGridViewDelegate(updateGridView);
            updDel.Invoke(e.DataTableInfo, pageNumber);
        }

        private void updateGridView(DataTable aTable, int pageNumber)
        {
            GridView1.DataSource = aTable;
            if (GridView1.AllowPaging)
                GridView1.PageIndex = pageNumber;

            GridView1.DataBind();
        }

        protected void GridView1_OPIC(object sender, GridViewPageEventArgs e)
        {
         
            pageNumber = e.NewPageIndex;
            DBConnecting.DataObjects.DataObjects src = new DBConnecting.DataObjects.DataObjects();
            src.ReturnDataTable += new DBConnecting.EventHandlers.DataTableEventArgs.DataTableEventHandler(HandleDataTable);
            src.AsyncDataTableStart(query, CommandType.Text);
        }
    }



GridView1_OPIC运行的方法,HandleDataTable方法工作,方法updateGridView工作。

我仍​​然只在屏幕上看到gridview的第1页。如何在webbrowser中更新GridView? UpdatePanel是aspx文件中的GridView。




the method GridView1_OPIC runs, the method HandleDataTable works and the method updateGridView works.
Still I only see page 1 of the gridview on screen. How to have the GridView updated in the webbrowser? An UpdatePanel is surronding the GridView in the aspx file.

<asp:UpdatePanel runat="server" ID="upnlDataGrid" >
        <ContentTemplate>
            <div id="grid">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="true" EnableSortingAndPagingCallbacks="true"
                    PageSize="20" OnPageIndexChanging="GridView1_OPIC">
                </asp:GridView>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>



当UpdateMode设置为'Conditional'时,GridView1.Update()命令运行正常第一次,但是当分页给出的错误是:

'System.InvalidOperationException:Update方法只能在Render之前在ID为'upnlDataGrid'的UpdatePanel上调用。'



如何克服这个问题?


When the UpdateMode is set to 'Conditional' the GridView1.Update() command runs fine the first time, but when paging the error given is:
'System.InvalidOperationException: The Update method can only be called on UpdatePanel with ID 'upnlDataGrid' before Render.'

How to overcome this problem?

推荐答案

我自己修好了!

问题是。在使用异步模型时,页面呈现在异步调用完成之前完成。因此,当结果出现时,页面将被提交给客户端!



您必须使用轮询模型来解决此问题:

所以我的代码隐藏现在(并将进一步完善):

Fixed it myself!
The problem was. While using the asynchronous model the page rendering was finished before the asynchronous call was finished. So when the result was there the page would have been submitted to the client!

You must use the polling model to fix this problem:
so my codebehind is now (and will be further perfectionized):
public partial class WebForm1 : System.Web.UI.Page
    {
        bool finishedCall = false;
        int pageNumber = 0;
        delegate void updateGridViewDelegate(DataTable aTable, int pageNumber);
        string query = "select top 100 * from [dbo].[FactInternetSales]";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShootTheCall();
            }
        }

        private void ShootTheCall()
        {
            DBConnecting.DataObjects.DataObjects src = new DBConnecting.DataObjects.DataObjects();
            src.ReturnDataTable += new DBConnecting.EventHandlers.DataTableEventArgs.DataTableEventHandler(HandleDataTable);
            src.AsyncDataTableStart(query, CommandType.Text);
        }

        private void HandleDataTable(object sender, DataTableEventArgs e)
        {
           
            DataTable table = e.DataTableInfo;
            updateGridViewDelegate updDel = new updateGridViewDelegate(updateGridView);
            updDel.Invoke(e.DataTableInfo, pageNumber);
        }

        private void updateGridView(DataTable aTable, int pageNumber)
        {
            GridView1.DataSource = aTable;
            if (GridView1.AllowPaging)
                GridView1.PageIndex = pageNumber;

            GridView1.DataBind();

            Label lblPageNr = (Label)Page.Master.FindControl("lblPageNr");
            if (lblPageNr != null)
                lblPageNr.Text = string.Format("PageNr set = {0}", pageNumber);

            finishedCall = true;
        }

        protected void GridView1_OPIC(object sender, GridViewPageEventArgs e)
        {
            pageNumber = e.NewPageIndex;
            ShootTheCall();
            while (!finishedCall)  // waiting till async call is finished
            {

            }
        }


请用以下代码替换你的html

Please replace your html with below code
<asp:UpdatePanel runat="server" ID="upnlDataGrid"     UpdateMode="Conditional">
        <contenttemplate>
            <div id="grid">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="true" EnableSortingAndPagingCallbacks="true"

                    PageSize="20" OnPageIndexChanging="GridView1_OPIC">
                
            </div>
        </contenttemplate>

在你的代码背后请替换你的 updateGridView 使用以下代码的方法:





And in your code behind please replace your updateGridView method with below code:


private void updateGridView(DataTable aTable, int pageNumber)
       {
           GridView1.DataSource = aTable;
           if (GridView1.AllowPaging)
               GridView1.PageIndex = pageNumber;

           GridView1.DataBind();
           //update your update panel manually.
           upnlDataGrid.update();
       }



希望它有所帮助。


Hope it helped.


我有同样的问题。请帮助。

没有母版页,它可以正常工作,但是当使用母版页时,它只能在第一次工作,第二次不起作用。请帮助解决如何修复。
i have same problem.please help.
Without master page,it work normally,but when using master page,it work only first time,second time does not work.please help how to fixed.


这篇关于GridView中的分页不适用于异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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