Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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);
        }
    }

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.

aspx
<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>

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?
Posted

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):
C#
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
            {

            }
        }
 
Share this answer
 
Please replace your html with below code
ASP.NET
<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>
And in your code behind please replace your updateGridView method with below code:


C#
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.
 
Share this answer
 
v4
Comments
Herman<T>.Instance 31-Aug-11 3:37am    
IF you would have read all the text you should have seen the last part about:
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.'
Herman<T>.Instance 31-Aug-11 8:24am    
hoping/waiting for a good answer
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900