views:

944

answers:

2

I am using ASP.NET 2.0 with AJAX Extensions (1.0?) and am wondering if it is possible to call a method asynchronously and have the results populate on the page after it has been loaded.

I have a gridview that is populated by a fairly long-running SQL query. I would prefer to have the page come up and the results trickle back in as they are returned from the server instead of forcing the user to stare at a blank page until everything is processed.

+1  A: 

You can use an asp:UpdatePanel and insert the gridview in there. They just call the AJAX call during load. You use the Sys.Application.load event. Check it out here for more information: http://www.asp.net/ajax/documentation/live/overview/AJAXClientEvents.aspx

Tom
A: 

You can place a hidden button inside your updatepanel and do a PostBack to that button. Is not an elegant solution but it works fine.

Inside your updatepanel you will write something like this.

<div style="visibility:hidden">
  <asp:Button ID="btnLoad" OnClick="btnLoad_Click" runat="server"/>
</div>

On your Page_Load event you must register the script for the PostBack:

protected void Page_Load(object sender, EventArgs e)
{    
    if (!Page.IsPostBack)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "InitialLoad" + this.ClientID, Page.ClientScript.GetPostBackEventReference(btnLoad, "")+";", true);
    }
}

Then you can write the code that will be executed on startup in the btnLoad_Click method (OnClick event for the button).

I tried another approach using a AJAX Timer and disabling it on the first Tick but sometimes I receive more than one tick before the code behind is executed, so I discarded that solution.

nicoruy