ASP.NET 3.5: Display UpdateProgress during Page_Load()

edosoft picture edosoft · May 26, 2009 · Viewed 23k times · Source

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.

Answer

Jim Dawson picture Jim Dawson · Feb 15, 2013
  1. Move your page_load code into a new function.
  2. Add a AJAX timer into the ContentTemplate section of your page. Set the interval to 500. (1/2 second)
  3. Double-click on the Timer object in Design view to create a _tick handler.
  4. In the _tick handler created in the previous step, call the following code

    protected void My_Timer_Tick(object sender, EventArgs e)
    {
        My_Timer_Name.Enabled = false;
        My_Page_Load_Function(); // Function created in step 1 above)
    }
    
    protected void My_Page_Load_Function()
    {
        System.Threading.Thread.Sleep(5000); // A delay to simulate doing something.
        lblMyLabel.Text = "Done!";  // Write output to page. 
    }