Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General ASP.NET >  Displaying the progress in a popup-window while waiting
Add to MyHood
   Displaying the progress in a popup-window while waiting   [ printer friendly ]
Stats
  Rating: 3.67 out of 5 by 3 users
  Submitted: 04/17/03
Bart Coppens ()

 
Introduction
When you are creating a heavy application which has a lot of processing to do before the user can continue, you may want the user to know about the progress. This tutorial is not about how to find out the progress, but about how to display it in a popup-window. I assume you can find a way yourself to indicate the percentage of progress of your process.

Use a HttpHandler
When you try to open a popup-window with a ASP .NET-page in it, you will see that the window will be opened, but the content isn't displayed until your process is ready. That's not wat we want, so we will have to find another way. The way I used and proved to work is creating a HttpHandler. You can do this by adding a section to the <system.web>-part of your web.config:

<httpHandlers>
 <add verb="GET" path="ProgressHandler.aspx" 
  type="ProgressCounter.ProgressHandler,ProgressCounter" 
  validate="false" />       
</httpHandlers>
>

Now a handler is created which redirects every request to ProgressHandler.aspx to the class ProgressHandler in the ProgressCounter-namespace.

Save the progress in the Application-state
To make sure the popup-window can reach your information about the progress, you have to save it in the Application-state. You would like to save it in the Session of course, but this information doesn't seem to be updated until the whole proces is done. You will need to create a unique key to make sure not every user gets the same progress-counter. For every session the Session-ID is unique, so let's use that to create a unique key. This is the code that is activated when a button is clicked to start the proces (my proces is sleeping 100 times for 300 millisecs):
private void StartProcesButton_Click(object sender, System.EventArgs e)
{
 // Start the proces
 for(int i=0; i<=100; i++) 
 {
  System.Threading.Thread.Sleep(300);
  // Save the progress in the Application-state with a unique key for this session
  Application["progress_" + Session.SessionID] = i;
 }
 Response.Write("<h1>PROCES FINISHED!!</h1>");
}
}


Opening the popup-window
Opening another window can be done using JavaScript. To be sure the popup-window knows what key to use, we pass the unique Session-ID to the popup-window. This piece of JavaScript could be registered and bound to the button (the client-side OnClick is handled before the server-side OnClick) in the Page_Load-method and could look something like this:
private void Page_Load(object sender, System.EventArgs e)
{
 // Create a string with the JavaScript
 string jsString = "<SCRIPT language='JavaScript'>";
 jsString += "window.open('ProgressHandler.aspx?SessionID=";
 // Pass the Session-ID as a parameter
 jsString += Session.SessionID + "',null,'');";
 jsString += "</SCRIPT>";
 // Register this function to the Response
 RegisterClientScriptBlock("OpenPopup", jsString);
 // Bind this function to the button that starts the proces
 StartProcesButton.Attributes.Add("OnClick","javascript: LoadViewer();");
}
}


Reading and displaying the data in the popup-window
Now you'll have to write the class you referred to in the web.config. This class has to implement IHttpHandler. In the ProcessRequest-method you should fill your Responsestream. I created a page that refreshes itself (using meta-tags) every second, that's why the progress can be displayed! In the understanding example I used , but of course you can use every other control you want or just display a counter like "Progress: xx %":
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft;

namespace ProgressCounter
{
 public class ProgressHandler : IHttpHandler
 {
  public ProgressHandler()
  {            
  }

  public void ProcessRequest(System.Web.HttpContext context)
  {
   HtmlTextWriter writer = new HtmlTextWriter(context.Response.Output);
   // Build the HTML for the popup
   writer.WriteFullBeginTag("html");
   writer.WriteFullBeginTag("body");
   writer.WriteFullBeginTag("head");
   writer.WriteFullBeginTag("title");
   writer.Write("Progress");
   writer.WriteEndTag("title");
   writer.WriteBeginTag("meta");
   writer.WriteAttribute("http-equiv","refresh");
   writer.WriteAttribute("content","1");
   writer.Write(HtmlTextWriter.TagRightChar);
   writer.WriteEndTag("head");
   writer.WriteFullBeginTag("body");
            
   int progress;

   // When the process is unknown in the Application-state, the process is 0
   if(context.Application["progress_" + context.Request.Params["SessionID"]] == null) progress = 0;
   // Else this equals the int in the Application-state
   else progress = (int) context.Application["progress_" + context.Request.Params["SessionID"]];

   // Create the progressbar
   ProgressBar bar = new ProgressBar();
   bar.ID = "ProgressBar";
   // Set the value of the progressbar
   bar.PercentageOfProgress = progress;
   bar.RenderControl(writer);

   // If the proces is finished,
   if(progress == 100) 
   {
    // Close the popup-window using JavaScript
    writer.WriteBeginTag("script");
    writer.WriteAttribute("language","javascript");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("window.close();");
    writer.WriteEndTag("script");

    // DON'T FORGET to remove the variable from the Application-state, otherwise it will exist 'forever'    context.Application.Remove("progress_" + context.Request.Params["SessionID"]);
   }

   writer.WriteEndTag("body");
   writer.WriteEndTag("html");
  }

  public bool IsReusable
  {
   get
   {
    return true;
   }
  }
 }
}
}



Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
Just a little help. Try to format your code sections so that it's easier to read. You can use the [cs] tag for C#, C++ and other kind code, and I think there's an [asp] tag for ASP and ASP.NET code snippets. But beyond that, not a bad tutorial.
-- J J, April 28, 2003
 
Interesting stuff.. though it's not really a tutorial, more like a tool.. =)
-- Larry Mak, May 09, 2003
 
Copyright © 2001 DevHood® All Rights Reserved