Udostępnij za pośrednictwem


Introduction to the UpdateProgress Control

In this tutorial you will use UpdateProgress controls to display the progress of partial-page updates. If a page contains UpdatePanel controls, you can also include UpdateProgress controls to keep users informed about the status of partial-page updates. You can use one UpdateProgress control to represent the progress of partial-page updates for the whole page. Alternatively, you can include an UpdateProgress control for every UpdatePanel control. Both of these patterns are shown in this tutorial.

Prerequisites

To implement the procedures in your own development environment you need:

  • Microsoft Visual Studio 2005 or Visual Web Developer Express.

  • An AJAX-enabled ASP.NET Web site.

Using a Single UpdateProgress Control

You will begin by using a single UpdateProgress control to show the progress for all partial-page updates on the page.

To use a single UpdateProgress control for the whole page

  1. Create a new page and switch to Design view.

  2. In the AJAX Extensions tab of the toolbox, double-click the ScriptManager control to add it to the page.

  3. Double click the UpdatePanel control to add it to the page.

    UpdatePanel Tutorial

  4. Double-click the UpdateProgress control to add it to the page.

  5. Inside the UpdateProgress control, add the text Processing….

    UpdateProgress Tutorial

  6. Inside the UpdatePanel control add a Label control and a Button control.

  7. Set the Text property of the Label control to Initial Page Rendered.

    UpdateProgress Tutorial

  8. Double click the Button control to add a handler for the button's Click event.

  9. Add the following code to the Click handler, which artificially creates a three-second delay and then displays the current time.

    ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
        ' Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000)
        Label1.Text = "Page refreshed at " & _
            DateTime.Now.ToString()
    EndSub
    
    protectedvoid Button1_Click(object sender, EventArgs e)
    {
        // Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000);
        Label1.Text = "Page refreshed at " +
            DateTime.Now.ToString();       
    }
    

    Note

    The handler for the Click event intentionally introduces a delay for this tutorial. In practice, you would not introduce a delay. Instead, the delay would be the result of server traffic or of server code that takes a long time to process, such as a long-running database query.

  10. Save your changes and then press CTRL+F5 to view the page in a browser.

  11. Click the button.

    After a short delay, the progress message is displayed. When the handler for the Click event has finished, the progress message is hidden and the time that is displayed in the panel is updated.

    The example is styled to better show the region of the page that the UpdatePanel represents.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
            ' Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000)
            Label1.Text = "Page refreshed at " & _
                DateTime.Now.ToString()
        EndSub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdateProgress Tutorial</title>
        <style type="text/css">
        #UpdatePanel1 { 
          width:300px; height:100px;
         }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel</legend>
                    <asp:Label ID="Label1" runat="server" Text="Initial page rendered."></asp:Label><br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                <ProgressTemplate>
                    Processing...
                </ProgressTemplate>
            </asp:UpdateProgress>
    
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protectedvoid Button1_Click(object sender, EventArgs e)
        {
            // Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000);
            Label1.Text = "Page refreshed at " +
                DateTime.Now.ToString();       
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdateProgress Tutorial</title>
        <style type="text/css">
        #UpdatePanel1 { 
          width:300px; height:100px;
         }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel</legend>
                    <asp:Label ID="Label1" runat="server" Text="Initial page rendered."></asp:Label><br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                <ProgressTemplate>
                    Processing...
                </ProgressTemplate>
            </asp:UpdateProgress>
    
        </div>
        </form>
    </body>
    </html>
    

Using Multiple UpdateProgress Controls

One UpdateProgress control on the page can show a progress message for all UpdatePanel controls on the page. Asynchronous postbacks originating inside an UpdatePanel control cause the UpdateProgress control to display its message. Postbacks from controls that are triggers for the panel also display the message.

You can associate the UpdateProgress control with a single UpdatePanel control by setting the progress control's AssociatedUpdatePanelID property. In that case, the UpdateProgress control displays a message only when a postback originates inside the associated UpdatePanel control.

In the next procedure, two UpdateProgress controls are added to a page, each associated with a different UpdatePanel control.

To use multiple UpdateProgress controls on a page

  1. Create a new page and switch to Design view.

  2. In the AJAX Extensions tab of the toolbox, double-click the ScriptManager control to add it to the page.

  3. Double-click the UpdatePanel control two times to add two instances of the control to the page.

    UpdateProgress Tutorial

  4. In each UpdatePanel control, add a Label control and a Button control.

  5. Set the Text property of both Label controls to Panel Initially Rendered.

    UpdateProgress Tutorial

  6. Double-click each Button control to add a handler for each button's Click event.

  7. Add the following code to each Click handler, which artificially creates a three-second delay and then displays the current time.

    ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
        ' Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000)
        Label1.Text = "Page refreshed at " & _
            DateTime.Now.ToString()
    EndSubProtectedSub Button2_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
        ' Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000)
        Label1.Text = "Page refreshed at " & _
            DateTime.Now.ToString()
    EndSub
    
    protectedvoid Button1_Click(object sender, EventArgs e)
    {
        // Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000);
        Label1.Text = "Page refreshed at " +
            DateTime.Now.ToString();       
    }
    
    protectedvoid Button2_Click(object sender, EventArgs e)
    {
        // Introducing delay for demonstration.
        System.Threading.Thread.Sleep(3000);
        Label2.Text = "Page refreshed at " +
            DateTime.Now.ToString();       
    
    }
    
  8. Switch to Design view.

  9. Click inside the first UpdatePanel control and add an UpdateProgress control.

  10. Inside the UpdateProgress control, add the text Panel1 Updating.

    This sets the ProgressTemplate property.

  11. Select the UpdateProgress control, and in the Properties window, set the AssociatedUpdatePanelID property to UpdatePanel1.

    UpdateProgress Tutorial

  12. Click inside the second UpdatePanel control and add a second UpdateProgress control.

  13. Set the text of the UpdateProgress control to Panel2 Updating and set its AssociatedUpdatePanelID property to UpdatePanel2.

    UpdateProgress Tutorial

  14. Save your changes, and then press CTRL+F5 to view the page in a browser.

  15. Click the button in the first panel.

    After a short delay, the progress message associated with the first panel is displayed. The other UpdateProgress control is not displayed.

  16. Click the button in the second panel.

    The progress message associated with the second panel is displayed.

    Note

    By default, starting a new asynchronous postback while an earlier one is in progress cancels the first postback. For more information, see Giving Precedence to a Specific Asynchronous Postback.

    The example is styled to better show the region of the page that the UpdatePanel represents.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
            ' Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000)
            Label1.Text = "Page refreshed at " & _
                DateTime.Now.ToString()
        EndSubProtectedSub Button2_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
            ' Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000)
            Label1.Text = "Page refreshed at " & _
                DateTime.Now.ToString()
        EndSub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdateProgress Tutorial</title>
        <style type="text/css">
        #UpdatePanel1, #UpdatePanel2 { 
          width:300px; height:100px;
         }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel1</legend>
                    <asp:Label ID="Label1" runat="server" Text="Panel initially rendered."></asp:Label>
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
                        <ProgressTemplate>
                            Panel1 updating...
                        </ProgressTemplate>
                    </asp:UpdateProgress>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel2</legend>
                    <asp:Label ID="Label2" runat="server" Text="Panel initially rendered."></asp:Label>
                    <br />
                    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                    <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2">
                        <ProgressTemplate>
                            Panel2 updating....
                        </ProgressTemplate>
                    </asp:UpdateProgress>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
    
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protectedvoid Button1_Click(object sender, EventArgs e)
        {
            // Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000);
            Label1.Text = "Page refreshed at " +
                DateTime.Now.ToString();       
        }
    
        protectedvoid Button2_Click(object sender, EventArgs e)
        {
            // Introducing delay for demonstration.
            System.Threading.Thread.Sleep(3000);
            Label2.Text = "Page refreshed at " +
                DateTime.Now.ToString();       
    
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdateProgress Tutorial</title>
        <style type="text/css">
        #UpdatePanel1, #UpdatePanel2 { 
          width:300px; height:100px;
         }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel1</legend>
                    <asp:Label ID="Label1" runat="server" Text="Panel initially rendered."></asp:Label>
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
                        <ProgressTemplate>
                            Panel1 updating...
                        </ProgressTemplate>
                    </asp:UpdateProgress>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <fieldset>
                    <legend>UpdatePanel2</legend>
                    <asp:Label ID="Label2" runat="server" Text="Panel initially rendered."></asp:Label>
                    <br />
                    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                    <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2">
                        <ProgressTemplate>
                            Panel2 updating....
                        </ProgressTemplate>
                    </asp:UpdateProgress>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
    
        </div>
        </form>
    </body>
    </html>
    

Review

This tutorial introduced two ways to use the UpdateProgress control. The first way is to add one UpdateProgress control on the page that is not associated with any particular UpdatePanel control. In that case, the control shows a progress message for all UpdatePanel controls. The second way to use the progress control is to add multiple UpdateProgress controls and to associate each one with a different UpdatePanel control.

See Also

Tasks

Introduction to the UpdatePanel Control

Concepts

UpdateProgress Control Overview

Partial-Page Rendering Overview

Reference

UpdateProgress

UpdatePanel