ASP: Progress Bar


Introduction

Author: Martin Warning
Year: 2007
License: Free

This code shows an example of a progress bar that is displayed during the execution of an ASP cycle. With ASP the client browser is not updated until all ASP is executed. This doesn't allow any form of progress bar to displayed on the client side during the execution of our ASP code. The Response.Flush object allows us to send data to the client browser during the execution of the ASP code. The Response.Flush object will write whatever HTML (CSS & JavaScript as well) that have already been created up till the point the Response.Flush object is encountered. So the Response.Flush object does allow us to send information the client browser during the execution of our ASP code.

The following example shows an example code to show how this progress bar could be implemented. In this case we use a For loop with an arbitrary long cycle. This cycle will run at different speeds based on your server software. You would implement this code in your own cycles like a database loop cycle. If this code executes to quickly you can increase the value of intCycle.

Code

Languages: At the beginning of the document you will need to set the following:
<% Response.Buffer = True %>
The following code is an example cycle with progress bar:
<%
' Declare and initialize the variables
Dim intCycle : intCycle = 3000000
Dim i
Dim j : j = 0
Dim x : x = 0

	' i and intCycle are used for the cycle in this example

	' j is used for the 10% spacer image
	' x is used for the 1% bar image is

Response.Write("<img src=""progress.gif"" alt="""" /><br />")

' Start the cycle
For i = 1 To intCycle

	' Every 1% of completion a bar image is written to the page
	If x = (intCycle/100) Then
		Response.Write("<img src=""bar.gif"" alt=""% Completed"" width=""3"" height=""20"" />")
		x = 0
		If j = 10 Then
		' Every 10% of completion a spacer images is written to the page
			Response.Write("<img src=""bar_spacer.gif"" alt="""" width=""1"" height=""20"" />")
			j = 0
		Else
			j = j + 1
		End If

	' Write html to client browser
		Response.Flush

	Else
		x = x + 1
	End If
Next
%>


Printed from: http://flyinglowlander.com/ (5/20/2012)
© FlyingLowander.com 2006 - 2012

Visit http://flyinglowlander.com for more XHTML, CSS, ASP and JavaScript examples, templates and tutorials.