using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime startTime = System.DateTime.Now;
DateTime endTime = System.DateTime.Now;
// 根据 ProgressBar.htm 显示进度条界面
string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm");
StreamReader reader = new StreamReader(@templateFileName,System.Text.Encoding.GetEncoding("gb2312"));
string html = reader.ReadToEnd();
reader.Close();
Response.Write(html);
Response.Flush();
System.Threading.Thread.Sleep(1000);
string jsBlock;
// 处理完成
jsBlock = "<script>BeginTrans('开始处理...');</script>";
Response.Write(jsBlock);
Response.Flush();
System.Threading.Thread.Sleep(1000);
// 根据处理任务处理情况更新进度条
for (int i = 1; i <= 100; i++)
{
System.Threading.Thread.Sleep(20);
jsBlock = "<script>SetPorgressBar('" + "A" + i.ToString() + "','" + i.ToString() + "');</script>";
Response.Write(jsBlock);
Response.Flush();
}
// 处理完成
jsBlock = "<script>EndTrans('处理完成。');</script>";
Response.Write(jsBlock);
Response.Flush();
// 用时
endTime = DateTime.Now;
jsBlock = "<script>SetTimeInfo('用时" + GetTimeSpan(startTime,endTime) + "');</script>";
Response.Write(jsBlock);
Response.Flush();
}
protected string GetTimeSpan(DateTime startTime, DateTime endTime)
{
string timeSpan = string.Empty;
TimeSpan ts = endTime - startTime;
if (ts.Days > 0)
timeSpan += ts.Days.ToString() + "天";
if (ts.Hours > 0)
timeSpan += ts.Hours.ToString() + "小时";
if (ts.Minutes > 0)
timeSpan += ts.Minutes.ToString() + "分钟";
if (ts.Seconds > 0)
timeSpan += ts.Seconds.ToString() + "秒";
return timeSpan;
}
}
<html>
<head>
<title></title>
<script language="javascript">
//开始处理
function BeginTrans(msg)
{
WriteText("Msg1",msg);
}
//设置进度条进度
function SetPorgressBar(msg, pos)
{
ProgressBar.style.width= pos + "%";
WriteText("Msg1",msg + " 已完成" + pos + "%");
}
//处理结束
function EndTrans(msg)
{
if(msg=="")
WriteText("Msg1","完成。");
else
WriteText("Msg1",msg);
}
//设置时间信息
function SetTimeInfo(msg)
{
WriteText("Msg2",msg);
}
// 更新文本显示信息
function WriteText(id, str)
{
var strTag = '<font face="Verdana, Arial, Helvetica" size="2" color="#ea9b02"><B>' + str + '</B></font>';
if (document.all) document.all[id].innerHTML = strTag;
}
</script>
</head>
<body>
<table align="center" style="height:100%">
<tr style="height:45%"><td></td></tr>
<tr>
<td>
<div id="Msg1" style="height:16px;"><font face="Verdana, Arial, Helvetica" size="2" color="#ea9b02"><b>正在加载...</b></font></div>
<div id="ProgressBarSide" style="width:300px; color:Silver;border-width:1px; border-style:Solid;">
<div id="ProgressBar" align="center" style="height:20px; width:0%; background-color:#316AC5;"></div>
</div>
<div id="Msg2" style="height:16px;"><font face="Verdana, Arial, Helvetica" size="2" color="#ea9b02"><b></b></font></div>
</td>
</tr>
<tr style="height:50%"><td></td></tr>
</table>
</body>
</html>