教你制做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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值