功能:
将后台的add()函数中的i变量值返回到前台,以控制进度条长度。
前台html:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProgressBar.aspx.cs" Inherits="ProgressBar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>
function SendData(){
//arg=document.getElementById('TextBox1').value;
//document.getElementById('TextBox1').value='0';
<%=ClientScript.GetCallbackEventReference(this,"1","GetData",null) %>;
}
function GetData(arg){
var ret=arg.split(',');
document.getElementById('test').style.width=parseInt(ret[0])*10+"px";
document.getElementById('len').innerHTML=ret[0]+"%";
if(ret[1]=='NO')
setTimeout("SendData()",500);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="test" style="height:20px; background-color:Blue; width:0px;"></div><a id="len"></a><input id="Button1" type="button" value="button" οnclick="SendData()" /><br/>
</div>
</form>
</body>
</html>
后台cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
public partial class ProgressBar : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler//继承接口
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
th = null;
RetValue = 0;
}
}
private static Thread th;
private static int RetValue;
void ICallbackEventHandler.RaiseCallbackEvent(string Url)//实现方法
{
if (th == null)
{
th = new Thread(add);
//th.TrySetApartmentState(ApartmentState.STA);
th.Start();
}
}
string ICallbackEventHandler.GetCallbackResult()//实现方法
{
//在这里可以调用自己的方法,处理自己的过程
if (RetValue == 100)
return RetValue.ToString() + ",OK";
return RetValue.ToString() + ",NO";
}
void add()//控制RetValue值
{
for (int i=0; i <= 100;i++ )
{
RetValue=i;
Thread.Sleep(500);
}
}
}