Microsoft Knowledge Base Email Alertz

(837375) - Explains how to use JavaScript to show progress in the client browser for a long-running ASP.NET page.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 837375 - Last Review: May 17, 2007 - Revision: 2.4

How to show progress in the client browser for a long-running ASP.NET page

On This Page

INTRODUCTION

This step-by-step article describes how to show progress in the client browser for a long-running Microsoft ASP.NET page. You can call the following JavaScript procedures until the page is loaded. This action makes sure that the page is in the process of loading and that the browser does not have any problem getting to the page.
  • The StartShowWait and the ShowWait functions show "Loading..." text on the client browser until the server controls appear on the page.
  • The HideWait function hides this text as soon as the controls appear on the page.

Requirements

This article assumes that you are familiar with the following topics:
  • ASP.NET programming
  • Microsoft Visual Basic .NET or Microsoft Visual C# .NET
  • JavaScript programming
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Internet Information Services
  • Microsoft Visual Studio .NET
  • The Microsoft .NET Framework

Create an ASP.NET Application with server controls

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C# Projects or Visual Basic Projects under Project Types.
  4. Under Templates, click ASP.NET Web Application. By default, a page that is named WebForm1.aspx is created.
  5. Add a TextBox control or a Button control to the WebForm1.aspx page.

    Note You add this control so that you can see a control when your page is loaded.

Declare and then call JavaScript functions

Because ASP.NET page processing is not sequential, the controls would not be present in the output stream. However, the Response.Write function outputs data to the output stream immediately.
  1. Right-click the WebForm1.aspx page, and then click View Code. Add the following code in the declaration section of the WebForm1.aspx.cs or the WebForm1.aspx.vb file:

    Visual C# .NET code
    using System.Threading;
    Visual Basic .NET code
    Imports System.Threading
  2. Add the following code to the Page_Load event procedure:

    Visual C# .NET code
    Response.Write("<div id='mydiv' >");
    Response.Write("_");
    Response.Write("</div>");
    Response.Write("<script>mydiv.innerText = '';</script>");
    
    Visual Basic .NET code
    Response.Write("<div id='mydiv' >")
    Response.Write("_")
    Response.Write("</div>")
    Response.Write("<script>mydiv.innerText = '';</script>")
  3. The following JavaScript functions that are named StartShowWait, ShowWait, and HideWait are used to write to the output stream until the page is loaded on the client browser:
    • The ShowWait function sets the text value of the <DIV> element to "Loading" followed by 10 periods ("..........").
    • The StartShowWait function calls the ShowWait function every second and then makes the <DIV> element visible on the browser.
    • The HideWait function hides the <DIV> element from being viewed on the browser.
    Write a statement to call the StartShowWait function, and then use the Response.Flush method to send the text value of the <DIV> element to the client. The HideWait function is called when the page has finished processing and is sent to the client. This function can also be called in the body onload event.

    To show the text "Loading" followed by 10 periods ("..........") on the client browser until the controls on the page are loaded, and then to hide this text as soon as the controls are rendered on the browser window, follow these steps:
    1. In the Page_Load event procedure of the Webform1.aspx.cs or the Webform1.aspx.vb file, add the following code:

      Visual C# .NET code
      Response.Write("<script language=javascript>;");
      Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
      Response.Write("{var output; output = 'Loading';dots++;if(dots>=dotmax)dots=1;");
      Response.Write("for(var x = 0;x < dots;x++){output += '.';}mydiv.innerText =  output;}");
      Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; 
      window.setInterval('ShowWait()',1000);}");
      Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';window.clearInterval();}");
      Response.Write("StartShowWait();</script>");
      Response.Flush();
      Thread.Sleep(10000) ;
      Visual Basic .NET code
      Response.Write("<script language=javascript>;")
      Response.Write("var dots = 0;var dotmax = 10;function ShowWait()")
      Response.Write("{var output; output = 'Loading';dots++;if(dots>=dotmax)dots=1;")
      Response.Write("for(var x = 0;x < dots;x++){output += '.';}mydiv.innerText =  output;}")
      Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; 
      window.setInterval('ShowWait()',1000);}")
      Response.Write("function HideWait(){mydiv.style.visibility = 
      'hidden';window.clearInterval();}")
      Response.Write("StartShowWait();</script>")
      Response.Flush()
      Thread.Sleep(10000)
    2. Switch to the HTML code editor for the WebForm1.aspx page, and then add the following code inside the <HEAD> tag:
      <script>
      HideWait();
      </script>
  4. On the Debug menu, click Start to run your application.
Note This code works in Microsoft Internet Explorer, but not with Netscape Navigator.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
http://msdn2.microsoft.com/en-us/library/65tcbxz3(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/65tcbxz3(vs.71).aspx)

http://msdn2.microsoft.com/en-us/library/wf922ts9(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/wf922ts9(vs.71).aspx)

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbwebbrowser kbwebforms kbscript kbhowtomaster KB837375
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Tittle Joseph - tittlej NOSPAM-AT-NOSPAM renewal-iis.com
Written: 4/14/2004 4:56 AM
Article is intrested but do you have any running demo (url) of this?

Anonymous User
Written: 4/14/2004 7:58 AM
Good Article, but does not work with Netscape :-(

Anonymous User
Written: 4/14/2004 8:41 AM
Pretty neat! Great little exercise for newcomers like me! Thanks!

Anonymous User
Written: 4/14/2004 3:00 PM
Neat but has a problem. It leaves a space at the top of the page. If your page cares about leaving 0 pixels margin from top then change in the code suggested by the article the HideWait script to do mydiv.style.display = 'none'; instead of setting visibility = 'hidden';

David
Written: 4/14/2004 7:12 PM
Some extra DOM-compliant javascript from http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_manip should make this work for all browsers. -dave

SITH - catnetpc NOSPAM-AT-NOSPAM hotmail.com
Written: 4/16/2004 1:31 PM
Excelent article, but do you have a running page url to test and seen the script in action

Pradeep - ypradeep23 NOSPAM-AT-NOSPAM hotmail.com Report As Irrelevant  
Written: 8/18/2009 1:41 AM
Nice logic. But, if you redirect the page from your server event, it is giving below error. "System.Web.HttpException: Cannot redirect after HTTP headers have been sent." Can anyone explore this? Thank you for your effort in advance.