Paging a GridView after data has returned asynchronously using <%@ Page Async="true"
<p>I was reading the following blogs about ASP.Net Async pages</p>

<ul>
<li><p><a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/magazine/cc163725.aspx</a></p></li>
<li><p><a href="http://world.episerver.com/Blogs/Magnus-Rahl/Dates/2012/1/Async-Pages-part-3-Async-Pages-with-databinding-and-events/" rel="nofollow">http://world.episerver.com/Blogs/Magnus-Rahl/Dates/2012/1/Async-Pages-part-3-Async-Pages-with-databinding-and-events/</a></p></li>
</ul>

<p>And a question popped on my head, please consider the following scenario:</p>

<ol>
<li>Assuming an Async page</li>
<li>The page registers async operations to retrieve data from a database in order to release immediately the ASP.Net working thread to increase scalability</li>
<li>The page passes paging info to these operations to paginate on the database server</li>
<li>The operation completes and the correct delegate is called on a new thread. (Not using a thread from the ASP.Net Thread Pool)</li>
<li>Data is returned to the page and can be bound to the <code>GridView</code> control on the <code>Page_PreRendercomplete</code></li>
</ol>

<p>At this point, I have the <strong>data paged on my page ready to be bound and displayed back to the user</strong> (returning only the records needed to be displayed <strong>and the number of Virtual Rows Count</strong>)</p>

<p>So with this information, I would like to bind it to my <code>GridView</code> control, but I have not figured it out <strong>how to display the paging results on my <code>GridView</code></strong></p>

<p>I tried using the following code:</p>

<pre><code>protected override void OnPreRenderComplete(EventArgs e)
{
    if (this.shouldRefresh)
    {
        var pagedSource = new PagedDataSource
        {
            DataSource = this.Jobs, 
            AllowPaging = true,
            AllowCustomPaging = false,
            AllowServerPaging = true,
            PageSize = 3,
            CurrentPageIndex = 0,
            VirtualCount = 20
        };

        this.gv.DataSource = pagedSource;
        this.gv.DataBind();
    }

    base.OnPreRenderComplete(e);
}
</code></pre>

<p>But the <code>GridView</code> control <em>simply ignores the <code>VirtualCount</code> property and the pager is never shown</em>, this is what I get:</p>

<p><img src="http://i.stack.imgur.com/3SlBW.png" alt="enter image description here"></p>

<h3>ASPX</h3>

<pre><code>&lt;%@ Page Async="true" AsyncTimeout="30"  ....
...
    &lt;asp:GridView runat="server" ID="gv" DataKeyNames="job_id" 
        AllowPaging="true" PageSize="3"
    &gt;
        &lt;Columns&gt;
            &lt;asp:CommandField ShowSelectButton="true" /&gt;
        &lt;/Columns&gt;
        &lt;SelectedRowStyle Font-Bold="true" /&gt;
    &lt;/asp:GridView&gt;
</code></pre>

<h3>ASPX Code behind</h3>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.shouldRefresh = true;
    }
}

public IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback callback, object state)
{
    var operation = new MyClassResult(callback, Context, state);
    operation.StartAsync();
    return operation;
}

public void EndAsyncOperation(IAsyncResult result)
{
    var operation = result as MyClassResult;
    this.Jobs = operation.Jobs;
}
</code></pre>

<p>Notes:</p>

<ul>
<li><p>I'm not interested on jQuery async posts to the server to get the data</p></li>
<li><p><code>MyClassResult</code> implements <code>IAsyncResult</code> and returns data from the database server</p></li>
<li><p><strong>I would love to use an <code>ObjectDataSource</code> if possible</strong></p></li>
</ul>
