asp.net+ajax多线程进度条的实现

最近写进度条写的有点上瘾,这次弄了个ajax的(*^_^*)
由于之前的那篇( http://www.cnblogs.com/suntears/archive/2007/04/24/724833.html)已经对前台如何控制图片进行了演示,这次就仅仅做功能模拟。别忘了打开页面的时候加上start参数,这样才可以启动查数程序。

前台代码:

None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >  
None.gif
< html >
None.gif  
< head >
None.gif    
< title > ajaxguagedemo </ title >
None.gif    
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif    
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif    
< meta  name =vs_defaultClientScript  content ="JavaScript" >
None.gif    
< meta  name =vs_targetSchema  content ="http://schemas.microsoft.com/intellisense/ie5" >
ExpandedBlockStart.gifContractedBlock.gif    
< script  language =javascript  > dot.gif
InBlock.gif
function returnresponse()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    urls
="ajaxguagedemo.aspx?guage=1";
InBlock.gif    
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
InBlock.gif    xmlHttp.open(
"GET",urls,true);
ExpandedSubBlockStart.gifContractedSubBlock.gif    xmlHttp.onreadystatechange
=function()dot.gif{
InBlock.gif        
if(xmlHttp.readyState==4)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            temp
=xmlHttp.responseText;
InBlock.gif            document.getElementById(
"ddf").innerText=temp;
InBlock.gif            
if(temp!="100%")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            returnresponse();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    xmlHttp.send(urls);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif    
</ script >
None.gif  
</ head >
None.gif  
< body  MS_POSITIONING ="GridLayout" >
None.gif    
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif    
< input  type =button  onclick ="returnresponse()"  value =1111111 >
None.gif    
< div  id =ddf ></ div >
None.gif    
</ form >
None.gif  
</ body >
None.gif
</ html >
None.gif

后台代码如下:

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.Threading;
None.gif
namespace  guagedemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ajaxguagedemo 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ajaxguagedemo : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static public int percent=0;//静态变量,记录百分比
InBlock.gif
        private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(Request.QueryString["start"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                startThread();
//开始新进程处理查数程序
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
if(Request.QueryString["guage"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ajaxResponse();
//响应ajax,返回百分比
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void guage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int i=0;i<100;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                percent
++;
InBlock.gif                Thread.Sleep(
500);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void startThread()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Threading.Thread thread
=new System.Threading.Thread(new System.Threading.ThreadStart(guage));
InBlock.gif            thread.Start();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ajaxResponse()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(percent.ToString()
+"%");
InBlock.gif            Response.Flush();
InBlock.gif            Response.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

很简单,看页面说明足够理解了。
附上Demo

转载于:https://www.cnblogs.com/suntears/archive/2007/04/24/725331.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值