Click here to Skip to main content
Click here to Skip to main content

A simple progress bar web user control

By , 8 Sep 2003
 

Sample Image - ProgressBar.gif

Introduction

In a recent project, I needed a way to show progress to a user in a series of manually progressed web pages. If they were on page 10 of 20 pages, I wanted them to be able to clearly see that they were half way done.

My ideal solution would be a quick, simple, lightweight progress bar. I toyed with complex ideas like dynamically creating images to represent the current state, but in the end another programmer showed me a much simpler way, by using an HTML table. I took that idea, and extended it by creating a web user control that I could use (and re-use) in my ASP.NET application.

Using the code

I created a user control with a single label control called lblProgress. I then added a public SetProgress method to allow the parent web form to set the progress.

Public Sub SetProgress(ByVal current As Integer, ByVal max As Integer)
    Dim percent As String = Double.Parse(current * 100 / max).ToString("0")

    If Not percent.Equals("0") And Not percent.Equals("100") Then
        lblProgress.Text = percent + "% complete (" + _
                current.ToString() + " of " + max.ToString() + ")"

    lblProgress.Text = lblProgress.Text & _
        "<TABLE cellspacing=0 cellpadding=0 border=1 width=200 ID="Table1"_
        ><TR><TD bgcolor=#000066 width=" + _
        percent.ToString() + _
        "%> </TD><TD bgcolor=#FFF7CE> _
        </TD></TR></TABLE>"
    End If
End Sub

Later on, I added a check to prevent the progress bar from displaying unless it reached a threshold (25% in my case). The idea was that we did not want to discourage the user by showing them that they were only 5% done. Rather, we would start showing the progress bar once they had reached some predetermined point, at which time it would become more of an encouragement.

If Integer.Parse(percent) < 25 Then
    'only start showing at 25%
    lblProgress.Visible = False
Else
    lblProgress.Visible = True
End If

Points of interest

Obviously this is a very simple technique, but it saved me from myself - I could have spent many hours writing a user control that dynamically generated an image, and obtained something only marginally better looking than this.

History

  • First upload: 9/9/2003.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Steven Campbell
Web Developer
United States United States
Member
Steve is a software developer working in Minneapolis, MN.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionQuestionmembersheraziii12 Sep '05 - 0:54 
Can any body tell me about usage of this control
 
sheraz
Generallbl doesnt refreshsussKov27 Nov '04 - 13:05 
my label only showes when the process is finished, i dont see the actual process
any idea why this is?
 

GeneralRe: lbl doesnt refreshmemberSteven Campbell28 Nov '04 - 7:11 
This progress bar is for showing progress in a multi-step process, like a questionnaire with lots of questions. It sounds like you are trying to use it to show progress when the user is waiting for something. It will not work that way.
 

my blog
Generalerrormemberwebber12345625 Nov '04 - 23:43 
Compiler Error Message: BC30182: Type expected.
 
Source File: \codeproject\Default.aspx.vb Line: 6
GeneralRe: errormemberSteven Campbell26 Nov '04 - 6:10 
See my response to your other message...
 

my blog
GeneralRe: error remainsmemberwebber12345626 Nov '04 - 8:29 
I made the other change and it seems to have resolved the other problem, but this error still occurs.
 

Generalerrormemberwebber12345625 Nov '04 - 23:39 
Parser Error Message: The base type 'SampleProgress.ProgressBar' does not exist in the source file 'ProgressBar.ascx.vb'.
 
Source Error:
 
Source File: \codeproject\ProgressBar.ascx Line: 1
GeneralRe: errormemberSteven Campbell26 Nov '04 - 6:09 
If you are copying the progress bar to your own project, then you need to change the following line in the ascx:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="ProgressBar.ascx.vb" Inherits="SampleProgress.ProgressBar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
to
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="ProgressBar.ascx.vb" Inherits="YOURPROJECTNAME.ProgressBar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

 

my blog
QuestionCompile?memberDOTofLTC3 Nov '04 - 7:39 
I am learning .NET so please excuse me if this is an elementary question. I downloaded the code to this project and just trying to run it I am getting
 
"Could not load type 'SampleProgress.ProgressBar'"
 
Is there something I must do? Compile something on the server, or ?
 
Thanks in advance for your help.
 
JWP
AnswerRe: Compile?memberSteven Campbell3 Nov '04 - 8:27 
Yes, the code is "code-behind", so it needs to be compiled into a DLL, which should placed in the /bin directory to work.
 
If you have merged the code into your own project, then you should change the first line of the .ascx file to use your correct namespace. Change Inherits="SampleProgress.ProgressBar" to Inherits="YourProject.ProgressBar".
 
Good luck.
 

my blog

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 9 Sep 2003
Article Copyright 2003 by Steven Campbell
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid