教你制做Web实时进度条

网上已经有很多Web进度条的例子,但是很多都是估算时间,不能正真反应任务的真实进度。我自己结合多线程和ShowModalDialog制做了一个实时进度条,原理很简单:使用线程开始长时间的任务,定义一个Session,当任务进行到不同的阶段改变Session的值,线程开始的同时使用ShowModalDialog打开一个进度条窗口,不断刷新这个窗口获取Session值,反应出实时的进度。下面就来看看具体的代码:(文章结尾处下载源代码)

先新建一个Default.aspx页面,
客户端代码:

None.gif
<
body MS_POSITIONING
=
"
GridLayout
"
>

None.gif    

<
form id
=
"
Form1
"
 method
=
"
post
"
 runat
=
"
server
"
>

None.gif            

<
br
>

None.gif            

<
br
>

None.gif            

<
asp:Button id
=
"
Button1
"
 runat
=
"
server
"
 Text
=
"
Start Long Task!
"
></
asp:Button
>

None.gif    

</
form
>

None.gif

</
body
>

服务器端代码:

None.gif
using
 System;
None.gif

using
 System.Collections;
None.gif

using
 System.ComponentModel;
None.gif

using
 System.Data;
None.gif

using
 System.Drawing;
None.gif

using
 System.Web;
None.gif

using
 System.Web.SessionState;
None.gif

using
 System.Web.UI;
None.gif

using
 System.Web.UI.WebControls;
None.gif

using
 System.Web.UI.HtmlControls;
None.gif

using
 System.Text;
None.gif
None.gif

namespace
 WebProgressBar
ExpandedBlockStart.gifContractedBlock.gif

dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for _Default.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class _Default : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Put user code to initialize the page here
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void LongTask()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//模拟长时间任务
InBlock.gif            
//每个循环模拟任务进行到不同的阶段
InBlock.gif
            for(int i=0;i<11;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Threading.Thread.Sleep(
1000);
InBlock.gif                
//设置每个阶段的state值,用来显示当前的进度
InBlock.gif
                Session["State"= i+1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//任务结束
InBlock.gif
            Session["State"= 100;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void OpenProgressBar(System.Web.UI.Page Page)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder sbScript 
= new StringBuilder();
InBlock.gif
InBlock.gif            sbScript.Append(
"<script language='JavaScript' type='text/javascript'>\n");
InBlock.gif            sbScript.Append(
"<!--\n");
InBlock.gif            
//需要IE5.5以上支持
InBlock.gif
            sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight: 100px; dialogWidth: 350px; edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No;');\n");
InBlock.gif            
//IE5.5以下使用window.open
InBlock.gif            
//sbScript.Append("window.open('Progress.aspx','', 'height=100, width=350, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');\n");
InBlock.gif
            sbScript.Append("// -->\n");
InBlock.gif            sbScript.Append(
"</script>\n");
InBlock.gif
InBlock.gif            Page.RegisterClientScriptBlock(
"OpenProgressBar", sbScript.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Threading.Thread thread
=new System.Threading.Thread(new System.Threading.ThreadStart(LongTask));
InBlock.gif            thread.Start();
InBlock.gif
InBlock.gif            Session[
"State"]=1;
InBlock.gif            OpenProgressBar(
this.Page);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


None.gif

新建一个进度条页面Progress.aspx

客户端:

在head中加入<base target="_self">

None.gif
<
body MS_POSITIONING
=
"
GridLayout
"
>

None.gif        

<
form id
=
"
Form1
"
 method
=
"
post
"
 runat
=
"
server
"
>

None.gif            

<
asp:Label id
=
"
lblMessages
"
 runat
=
"
server
"
></
asp:Label
>

None.gif            

<
asp:Panel id
=
"
panelBarSide
"
 runat
=
"
server
"
 Width
=
"
300px
"
 BorderStyle
=
"
Solid
"
 BorderWidth
=
"
1px
"

None.gif                ForeColor

=
"
Silver
"
>

None.gif                

<
asp:Panel id
=
"
panelProgress
"
 runat
=
"
server
"
 Width
=
"
10px
"
 BackColor
=
"
Green
"
></
asp:Panel
>

None.gif            

</
asp:Panel
>

None.gif            

<
asp:Label id
=
"
lblPercent
"
 runat
=
"
server
"
 ForeColor
=
"
Blue
"
></
asp:Label
>

None.gif        

</
form
>

None.gif

</
body
>

服务器端:

None.gif
using
 System;
None.gif

using
 System.Collections;
None.gif

using
 System.ComponentModel;
None.gif

using
 System.Data;
None.gif

using
 System.Drawing;
None.gif

using
 System.Web;
None.gif

using
 System.Web.SessionState;
None.gif

using
 System.Web.UI;
None.gif

using
 System.Web.UI.WebControls;
None.gif

using
 System.Web.UI.HtmlControls;
None.gif
None.gif

namespace
 WebProgressBar
ExpandedBlockStart.gifContractedBlock.gif

dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Progress.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Progress : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label lblMessages;
InBlock.gif        
protected System.Web.UI.WebControls.Panel panelProgress;
InBlock.gif        
protected System.Web.UI.WebControls.Panel panelBarSide;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblPercent;
InBlock.gif    
InBlock.gif        
private int state = 0;
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Put user code to initialize the page here
InBlock.gif
            if(Session["State"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state 
= Convert.ToInt32(Session["State"].ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Session[
"State"]=0;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(state>0&&state<=10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.lblMessages.Text = "Task undertaking!";
InBlock.gif                
this.panelProgress.Width = state*30;
InBlock.gif                
this.lblPercent.Text = state*10 + "%";
InBlock.gif                Page.RegisterStartupScript(
"","<script>window.setTimeout('window.Form1.submit()',100);</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(state==100)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.panelProgress.Visible = false;
InBlock.gif                
this.panelBarSide.Visible = false;
InBlock.gif                
this.lblMessages.Text = "Task Completed!";
InBlock.gif                Page.RegisterStartupScript(
"","<script>window.close();</script>");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


None.gif

源代码下载:
WebProgressBar.rar

转载于:https://www.cnblogs.com/JusticFu/archive/2006/06/29/438433.html

THE END
< <上一篇
下一篇>>