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   
GeneralMy vote of 4memberLeahAtWork24 Apr '12 - 17:54 
Simple and effective, even in 2012.
GeneralThanks lot!!!!!!!!memberkalim shekh13 Jan '11 - 20:26 
it is exat solution what i want.
 
its good idea behind that code..
 
again Thanks...
Generalthanks.../memberdakshithaw14 Dec '08 - 23:11 
thanks... it exactly is what i required... Smile | :)
QuestionClient-Sever interaction .netmember0700347H30 Nov '08 - 4:40 
I been trying to do a client-sever service game portal using VS 2005 web application using ASP.NET and C# .
 
1.How do I do a chat dialog with client-sever interaction like how msn Messenger work?
2.How can I do a simple 2D game like utopia.
 
Hope that you can explain step by step as I'm a beginner which start programming since mid of 2008.
Thank you for looking into this. Hope to see your reply ASAP.
 
warmest regards,
Steve
GeneralTrying to convert to C# [modified]memberEkjon5 Nov '08 - 12:12 
I was trying to convert it to C# and could not get it to work. It runs, but doesn't show me the bar. So, I am using the original vb version and its working. Now, how do I associate with a method that is not in a webform, just in a class and I am calling that method to process a file in a webform.
 
Any idea? Thanks.
 
modified on Wednesday, November 5, 2008 6:49 PM

QuestionWebprogressbar and firefoxmemberbrambillaa9 Oct '07 - 23:34 
I have used it with IE and it works fine. I have tested it with Firefox, but the progressbar scroll with the webpage. Can anyone help me?
 
Ambro
Generalneed helpmemberfaycal_2226 Aug '07 - 1:31 
Good morning
I have to develop a web application using ASP.NET and C# . I want to include a Progress Bar in ASP page, in order to show the progressing of a task that I choose from control «DropDownList ». Can you help me by explaining step by step because I am beginner.

Generalthxmemberastena10 Apr '07 - 22:42 
thx, this was very helpful
GeneralRe: thxmemberCertain Shadow22 Jul '07 - 14:25 
Indeed. Good value, thank you.
GeneralRetrieving the user control progressmemberw_mainwaring5 Feb '07 - 3:41 
Hi,
Thanks for posting this really useful example.
I have converted 'SetProgress' into a property, but am wondering how I can GET the progress control's value directly from the table? (ie. without storing the value in a member variable when it's set).
 
I have replaced:

with

 
This VB doesn't seem to work on the server when the form is posted back with a submit button:
Dim table as Table = FindControl("myTable")
 
as table is always Nothing
 
Can anyone tell me why please?
I am looking for help generally on how to retrieve data from a user control, which has been created by setting the 'Text' field to TABLE, just like in this example.
Any help would be gratefully appreciated.
Regards,
Will.

GeneralRe: Retrieving the user control progressmemberArkonXX8 Mar '07 - 3:11 
I think you only have to do is add runat="server" to this line:
TABLE ID="myTable" cellspacing=0 cellpadding=0 border=1 width=200 runat="server">
 
You're problem with not finding your control should be over.
 

 
ArkonXX
Just A Programmer
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
GeneralWill this work with C#memberDushman13 Oct '03 - 11:34 
How do you replace use the line continuation with c#. Is it available or is it only in VB
 
Thanks,
Dush.
GeneralRe: Will this work with C#memberMike Ellison13 Oct '03 - 14:27 
The syntax in C# is that a statement (line) is ended with a semicolon; so you should be fine breaking a line. This should work for example:
void MyProc (string sArg1,
             string sArg2,
             string sArg3)
{
  Console.Write("Something to write - all on one line");
  Console.Write(string.Format("something else {0} split on two lines",
                               sArg1));
}
The underscore as a line continuation character is specific to VB syntax.
GeneralRe: Will this work with C#memberSteven Campbell13 Oct '03 - 17:29 
Also, in C# you can prefix a string with @, in which case you can embed carriage returns in the string itself, like so:
 
Console.Write(@"This is the first line
and this is the second");

AnswerRe: Will this work with C#memberkbomb9877 Nov '07 - 9:46 
Here is a conversion of the only function to C#. Create the user control manually and paste the following function in the ProgressBar.ascx.cs file:
 
public void SetProgress(int current, int max)
{
string percent = ((current * 100) / max).ToString("0");
 
if (!percent.Equals("0"))
{
lblProgress.Text = percent + "% complete (Page " + current.ToString() + " of " + max.ToString() + ")";
lblProgress.Text += "";
}
 
lblProgress.Text += "
";

if (!percent.Equals("100"))
{
lblProgress.Text += "
 
";
}
 
if (Convert.ToInt32(percent) > 0)
{
lblProgress.Visible = true;
}
else
{
lblProgress.Visible = false;
}
}

GeneralDivs..memberRocky Moore11 Sep '03 - 16:33 
If you wanted to include the text inside the progress bar, you could use DIVs.
 
Example:
 
<div style="border-style:solid; border-width:1; position:relative; width: 
200px; height: 20px;"><br>
<div style="z-index: 3; width:100%; height:20px; position:absolute; top:0px; 
left:0px; text-align: center;"> Process 25% Complete </div><br>
<div style="background-color: red; z-index:1; width: 50px; position:absolute; 
left:0px; top:0px;height:20px;"></div><br>
<div style="background-color: yellow; z-index:1; width: 150px; position:absolute; 
left:50px; top:0px;height:20px;"></div><br>
</div>
 
Thus:
 

Process 25% Complete
 

 
Rocky Moore <><

GeneralRe: Divs..memberSteven Campbell12 Sep '03 - 2:40 
Thats a great suggestion. At the time I wrote the code, my CSS knowledge was extremely limited, but every day I see new and wonderful uses like this Big Grin | :-D
GeneralRe: Divs..memberSilver Hawk3 Jul '04 - 1:29 
Yes i agree
 

thanks for that.
GeneralPostbackmemberNick Seng9 Sep '03 - 15:24 
I've been thinking about using a progress bar in my web app too, but I've always had one issue with it; Postbacks. Won't the progress bar cause constant postback?
 










Support Bone

GeneralRe: PostbackmemberDukeyToo9 Sep '03 - 17:09 
It sounds like what you are thinking of is more like a progress bar that dynamically indicates how far some server process has gone. That would be much more intense than my simple example! It would either involve multiple server round trips, or at the least some javascript.
 
My progress bar example was intended to fulfill the much simpler need of a series of manually progressed web pages, to indicate to the user how far they are in the multi-step process.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 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