| |
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)
{
for(int i=0; i<=100; i++)
{
System.Threading.Thread.Sleep(300);
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)
{
string jsString = "<SCRIPT language='JavaScript'>";
jsString += "window.open('ProgressHandler.aspx?SessionID=";
jsString += Session.SessionID + "',null,'');";
jsString += "</SCRIPT>";
RegisterClientScriptBlock("OpenPopup", jsString);
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);
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;
if(context.Application["progress_" + context.Request.Params["SessionID"]] == null) progress = 0;
else progress = (int) context.Application["progress_" + context.Request.Params["SessionID"]];
ProgressBar bar = new ProgressBar();
bar.ID = "ProgressBar";
bar.PercentageOfProgress = progress;
bar.RenderControl(writer);
if(progress == 100)
{
writer.WriteBeginTag("script");
writer.WriteAttribute("language","javascript");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write("window.close();");
writer.WriteEndTag("script");
}
writer.WriteEndTag("body");
writer.WriteEndTag("html");
}
public bool IsReusable
{
get
{
return true;
}
}
}
} |
}
|
|