Search code examples
asp.netpageloadupdateprogress

ASP.NET 3.5: Display UpdateProgress during Page_Load()


I am building an ASP.NET site using Visual Studio 2008 and have a page looking like this (stuff snipped)

<asp:Content ID="Content2" ContentPlaceHolderID="PageContentPlaceHolder" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            the page here..
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100">
        <ProgressTemplate>
            <div>
                <asp:Image ID="AjaxImage" runat="server" ImageUrl="Ajax.gif" />
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>
</asp:Content>

The page_load starts a long (>5s) process

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

How can I display the UpdateProgress while the LongRunningProcess is running? It does work when I move the LongRunningProcess() call to a button onclick handler.


Solution

  • Create a normal div that shows the Ajax.gif so it shows "processing" by default.

    In the javascript pageLoad() function, make a call back to the page using Ajax's PageMethods.

       function pageLoad(sender, args) {
                    PageMethods.getVersions(LoadVersionsCallback);
        }
    

    The method you are calling in your .aspx.cs file has to be static, it can take parameters and looks something like:

       [System.Web.Services.WebMethod]
        public static string getVersions()
        {
          StringBuilder sb = new StringBuilder();
           ... etc.
          return sb.ToString();
    
        }
    

    The javascript function that you specified when you called the method will run when the method completes. It will be passed the results. At the end of this function you hide the Ajax.gif div.

       function LoadVersionsCallback(result) {
        // do something with the results - I load a dropdown list box.
    
       ...etc. 
        // here is where you hide your div holding the Ajax.gif  
    
       }
    

    And then you work on making whatever it is you are doing run in less than 1 second....