There are already many examples of Web progress bars on the Internet, but many of them are time estimates and cannot reflect the real progress of the task. I made a real-time progress bar by combining multiple threads and showmodaldialog. The principle is simple: Use a thread to start a long task and define a session. When the task changes the session value at different stages, when the thread starts, use showmodaldialog to open a progress bar window, refresh the window to obtain the session value, and reflect the real-time progress. Next let's take a look at the specificCode:(ArticleDownload at the endSource code)
Create a new default. aspx page first,
Client code:
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Br>
<Br>
<Asp: button id = "button1" runat = "server" text = "Start long task! "> </ASP: button>
</Form>
</Body>
Server code:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. text;
Namespace webprogressbar
{
/** // <Summary>
/// Summary description for _ default.
/// </Summary>
Public class _ default: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button button1;
Private void page_load (Object sender, system. eventargs E)
{
// Put user code to initialize the page here
}
Web Form Designer generated code # region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/** // <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void longtask ()
{
// Simulate a long task
// Each Cyclic Simulation Task goes to different stages
For (INT I = 0; I <11; I ++)
{
System. Threading. thread. Sleep (1000 );
// Set the state value of each stage to display the current progress.
Session ["state"] = I + 1;
}
// Task ended
Session [& quot; State & quot;] = 100;
}
Public static void openprogressbar (system. Web. UI. Page)
{
Stringbuilder sbscript = new stringbuilder ();
Sbscript. append ("<script language = 'javascript 'Type = 'text/JavaScript '> \ n ");
Sbscript. append ("<! -- \ N ");
// Ie5.5 or above is required
Sbscript. append ("window. showmodaldialog ('ss ss. aspx ', '', 'dialogheight: 100px; dialogwidth: 350px; edge: raised; center: Yes; help: No; resizable: No; Status: No; scroll: No; '); \ n ");
// Use window. Open below ie5.5
// Sbscript. append ("window. open ('Progress. aspx ', '', 'height = 100, width = 350, toolbar = No, menubar = No, scrollbars = No, resizable = No, location = No, status = no'); \ n ");
Sbscript. append ("// --> \ n ");
Sbscript. append ("</SCRIPT> \ n ");
Page. registerclientscriptblock ("openprogressbar", sbscript. tostring ());
}
Private void button#click (Object sender, system. eventargs E)
{
System. Threading. Thread thread = new system. Threading. Thread (new system. Threading. threadstart (longtask ));
Thread. Start ();
Session ["state"] = 1;
Openprogressbar (this. Page );
}
}
}
Create a progress bar page. aspx
client:
Add
forecolor = "Silver">
server:
using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
namespace webprogressbar
{
/** //
// summary description for progress.
//
public class progress: system. web. UI. page
{< br> protected system. web. UI. webcontrols. label lblmessages;
protected system. web. UI. webcontrols. panel panelprogress;
protected system. web. UI. webcontrols. panel panelbarside;
protected system. web. UI. webcontrols. label lblpercent;
private int state = 0;
private void page_load (Object sender, system. eventargs e)
{< br> // put user code to initialize the page here
If (session ["state"]! = NULL)
{< br> state = convert. toint32 (session ["state"]. tostring ();
}< br> else
{< br> session ["state"] = 0;
}< br> If (State> 0 & State <= 10)
{< br> This. lblmessages. TEXT = "task undertaking! ";
This. panelprogress. width = State * 30;
This. lblpercent. TEXT = State * 10 + "%";
page. registerstartupscript ("", " ");
}< br> If (State = 100)
{< br> This. panelprogress. visible = false;
This. panelbarside. visible = false;
This. lblmessages. TEXT = "task completed! ";
page. registerstartupscript ("", " ");
}< BR >}
Web Form Designer generated code # region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/** // <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}