Click here to Skip to main content
6,929,467 members and growing! (14,582 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate License: The Code Project Open License (CPOL)

Progressbar for long running scripts

By Wayne Redelinghuys

Show a progress bar when your code runs for a long time.
C#, Windows, .NET, ASP.NET, Dev
Posted:21 Aug 2008
Views:9,522
Bookmarked:18 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.10 Rating: 3.25 out of 5
2 votes, 22.2%
1
1 vote, 11.1%
2

3
3 votes, 33.3%
4
3 votes, 33.3%
5

Red.jpg

Introduction

I have been looking, to no avail, for an easy way to show a progress bar in ASP.NET that can be updated with C# code-behind, so I built one and think it might be useful to other developers who need to show the progress of a long running code snippet.

Using the code

The progress bar can be placed on any ASP.NET form by just adding a few new functions to your existing code. The colour can be changed by just updating the image used to display the bar.

There are only two functions that need to be included. The first function is to draw the progress bar in a table:

public void CreateInterface()
{
    //Draw the progress page
    Response.Write("<table width=\"100%\" style=\"font-family:Arial; font-size:12px;" + 
                   "text-align:center\"><tr><td>" + 
                   "<div id='mydiv' style=\"border:solid 1px black\" >" + 
                   "<strong>Progressbar Demo</strong><br /><hr />" +
                   "<table border=\"0\" style=\"font-family:Arial;" + 
                   " font-size:12px;\" width=\"300px\">" + 
                   "<tr><td width=\"100%\" style=\"text-align:center\"" + 
                   "><div id=\"ProgressText\">0%</div>" + 
                   "</td></tr></table>" +
                   "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" " + 
                   "width=\"300px\" height=\"13\">" + 
                   "<tr><td id=\"indicator\" style=\"background-image:url('Progress" + 
                   ddlColour.SelectedItem.Text + ".jpg');" + 
                   " background-repeat:repeat-x\" width=\"0%\">" + 
                   "</td><td width=\"100%\"></td></tr>" + 
                   "</table></div></td></tr></table>");
    //Insert the scripts for updating
    Response.Write("<script language="\""javascript\">;");

    Response.Write("function ShowProgress(){}");

    Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible';" + 
                   " window.setInterval('ShowProgress()',1000);}");

    Response.Write("function HideWait(){mydiv.style.display" + 
                   " = 'none';window.clearInterval();}");

    Response.Write("function UpdateIndicator(NewValue) " + 
                   "{indicator.style.width = NewValue +\"%\";" + 
                   " ProgressText.innerText=NewValue + \"%\"}");

    Response.Write("StartShowWait();</script>");

    if (!IsPostBack)
    {
        //Hide if first load
        Response.Write("<script>HideWait();</script>");
    }

    Response.Flush();
}

The second function is to update the progress bar from your code:

public void UpdateProgressBar(double Text)
{
    Response.Write("<script>UpdateIndicator(\"" + 
             Convert.ToInt32(Text).ToString() + "\");</script>");
    Response.Flush();
}

License

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

About the Author

Wayne Redelinghuys


Member

Occupation: Web Developer
Company: Metrix Software Solutions
Location: South Africa South Africa

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
QuestionHow to update progress based on server side task status. Pinmembersudipta.india6:40 31 May '09  
GeneralWhy boxing? PinmemberGuido_d4:29 22 Aug '08  
GeneralRe: Why boxing? PinmemberWayne Redelinghuys4:53 22 Aug '08  
GeneralPlease Check your formatiing PinmemberAbhijit Jana0:22 22 Aug '08  
GeneralRe: Please Check your formatiing PinmemberWayne Redelinghuys4:59 22 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2008
Editor: Smitha Vijayan
Copyright 2008 by Wayne Redelinghuys
Everything else Copyright © CodeProject, 1999-2010
Web11 | Advertise on the Code Project