Easyhot.cs
--------------------------------------------------------
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Security.Cryptography;
/// <summary>
/// Easyhot 的摘要说明
/// </summary>
public class Easyhot
{
/// <summary>
/// 全局变量
/// </summary>
public static string progressstr = "";
/// <summary>
/// 产生进度条bar
/// </summary>
public static void BuildProgressHtml(Double n)
{
string progress = "";
try
{
progressstr = "Start";
Double total = n;
Double i = 0;
int k = 0;
for (i = 0; (i <= total); i++)
{
int proInt =Convert.ToInt32(((i / total) * 100));
progress = "";
progress += "<table><tr><td><table width=/"233/" height=/"14/" border=/"0/" cellpadding=/"1/" cellspacing=/"1/" style=/"borde" +
"r:solid 1px #009900;/"><tr>";
for (int j = 1; (j <= 100); j = (j + 10))
{
if ((proInt > j))
{
progress += "<td width=/"20/" height=/"10/" bgcolor=/"#009900/"></td>";
k = j;
}
}
if ((k == 91))
{
progress = (progress + ("</tr></table></td><td><font size=/"2/" face=/"Verdana,Arial/">"
+ (proInt + "%</font></td><tr></table>")));
}
else
{
progress = (progress + ("<td></td></tr></table></td><td><font size=/"2/" face=/"Verdana,Arial/">"
+ (proInt + "%</font></td><tr></table>")));
}
progressstr = progress.ToString();
System.Threading.Thread.Sleep(10);
}
progressstr = "Finished";
}
catch (Exception ex)
{
Msg.MessageOut(ex.Message);
}
}
}
---------------------------------------------------------------------------------------------------
test.aspx
---------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Easyhot_test" %>
<!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 id="Head1" runat="server">
<title>AJAX进度条演示</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="AJAX进度条演示" /><br />
<div id="msgdiv" runat="server">
</div>
</div>
<br />
</form>
</body>
</html>
---------------------------------------------------------------------------------------------------------------
test.aspx.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 Easyhot_test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string CallBack_eventArgument;
//返回客户端的处理结果
public string GetCallbackResult()
{
string ret = "";
string[] buf;
buf = CallBack_eventArgument.Split(',');
switch (buf[0])
{
case "show":
try
{
string status = Easyhot.progressstr.ToString();
string ReloadReuest;
if ((status == "Finished"))
{
ReloadReuest = "false";
Easyhot.progressstr = "";
}
else
{
ReloadReuest = "true";
}
ret = "show" + "<$,,,,,$>" + status + "<$,,,,,$>" + ReloadReuest;
}
catch (Exception ex)
{
return "MsgBox" + "<$,,,,,$>" + ex.Message;
}
break;
}
return ret;
}
//客户端传入值
public void RaiseCallbackEvent(string eventArgument)
{
CallBack_eventArgument = eventArgument;
string[] buf = eventArgument.Split(',');
switch (buf[0])
{
case "reload":
Thread th = new Thread(this.showprogress);
th.Start();
break;
}
}
public void showprogress()
{
Easyhot.BuildProgressHtml(1000);
}
protected void Page_Load(object sender, EventArgs e)
{
string receiver = "";
receiver += "/r/n" + "<script>";
receiver += "/r/n" + "function receiver(dat){";
receiver += "/r/n" + " if (dat!=null) {";
receiver += "/r/n" + " buf=dat.split(/'<$,,,,,$>/');";
receiver += "/r/n" + " if (buf[0]==/'show/') {";
receiver += "/r/n" + " document.all." + msgdiv.ClientID + ".innerHTML=buf[1];";
receiver += "/r/n" + " document.all." + this.Button1.ClientID + ".disabled=true;";
receiver += "/r/n" + " if (buf[2]==/'true/') {setTimeout(/'showData()/',100);document.all."+ this.Button1.ClientID + ".disabled=true;} ";
receiver += "/r/n" + " else {document.all." + this.Button1.ClientID + ".disabled=false;}";
receiver += "/r/n" + " }";
receiver += "/r/n" + " if (buf[0]==/'ErrMsg/') {alert(/'�/'+buf[1]);}";
receiver += "/r/n" + " if (buf[0]==/'MsgBox/') {alert(/'�:/'+buf[1]);}";
receiver += "/r/n" + " }";
receiver += "/r/n" + "}";
receiver += "/r/n" + "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "receiver", receiver);
string CallBackJob = "";
CallBackJob += "/r/n" + "<script>";
CallBackJob += "/r/n" + "function reload(){ ";
CallBackJob += "/r/n" + this.ClientScript.GetCallbackEventReference(this, "/'reload/'", "receiver", "");
CallBackJob += "/r/n" + "}";
CallBackJob += "/r/n" + "function showData(){ ";
CallBackJob += "/r/n" + this.ClientScript.GetCallbackEventReference(this, "/'show/'", "receiver", "");
CallBackJob += "/r/n" + "}";
CallBackJob += "/r/n" + "function startreload(){ ";
CallBackJob += "/r/n" + " reload();setTimeout(/'showData();/',1000);document.all." + this.Button1.ClientID + ".disabled=true;";
CallBackJob += "/r/n" + "}";
CallBackJob += "/r/n" + "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallBackJob", CallBackJob);
Button1.Attributes.Add("onclick", "startreload()");
}
}