最近写进度条写的有点上瘾,这次弄了个ajax的(*^_^*)
由于之前的那篇(http://www.cnblogs.com/suntears/archive/2007/04/24/724833.html)已经对前台如何控制图片进行了演示,这次就仅仅做功能模拟。别忘了打开页面的时候加上start参数,这样才可以启动查数程序。
前台代码:
后台代码如下:
很简单,看页面说明足够理解了。
附上Demo
由于之前的那篇(http://www.cnblogs.com/suntears/archive/2007/04/24/724833.html)已经对前台如何控制图片进行了演示,这次就仅仅做功能模拟。别忘了打开页面的时候加上start参数,这样才可以启动查数程序。
前台代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>ajaxguagedemo</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript >
function returnresponse()
{
urls="ajaxguagedemo.aspx?guage=1";
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET",urls,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
temp=xmlHttp.responseText;
document.getElementById("ddf").innerText=temp;
if(temp!="100%")
{
returnresponse();
}
}
}
xmlHttp.send(urls);
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type=button onclick="returnresponse()" value=1111111>
<div id=ddf></div>
</form>
</body>
</html>
<html>
<head>
<title>ajaxguagedemo</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript >
function returnresponse()
{
urls="ajaxguagedemo.aspx?guage=1";
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET",urls,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
temp=xmlHttp.responseText;
document.getElementById("ddf").innerText=temp;
if(temp!="100%")
{
returnresponse();
}
}
}
xmlHttp.send(urls);
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type=button onclick="returnresponse()" value=1111111>
<div id=ddf></div>
</form>
</body>
</html>
后台代码如下:
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.Threading;
namespace guagedemo
{
/// <summary>
/// ajaxguagedemo 的摘要说明。
/// </summary>
public class ajaxguagedemo : System.Web.UI.Page
{
static public int percent=0;//静态变量,记录百分比
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Request.QueryString["start"]!=null)
{
startThread();//开始新进程处理查数程序
}
if(Request.QueryString["guage"]!=null)
{
ajaxResponse();//响应ajax,返回百分比
}
}
private void guage()
{
for(int i=0;i<100;i++)
{
percent++;
Thread.Sleep(500);
}
}
public void startThread()
{
System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(guage));
thread.Start();
}
private void ajaxResponse()
{
Response.Write(percent.ToString()+"%");
Response.Flush();
Response.Close();
}
Web 窗体设计器生成的代码
}
}
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.Threading;
namespace guagedemo
{
/// <summary>
/// ajaxguagedemo 的摘要说明。
/// </summary>
public class ajaxguagedemo : System.Web.UI.Page
{
static public int percent=0;//静态变量,记录百分比
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Request.QueryString["start"]!=null)
{
startThread();//开始新进程处理查数程序
}
if(Request.QueryString["guage"]!=null)
{
ajaxResponse();//响应ajax,返回百分比
}
}
private void guage()
{
for(int i=0;i<100;i++)
{
percent++;
Thread.Sleep(500);
}
}
public void startThread()
{
System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(guage));
thread.Start();
}
private void ajaxResponse()
{
Response.Write(percent.ToString()+"%");
Response.Flush();
Response.Close();
}
Web 窗体设计器生成的代码
}
}
很简单,看页面说明足够理解了。
附上Demo