方法一、不带进度条
后台:
1、
<asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" Text="生成静态的进度条" OnClientClick="createload()" />
2、
<script type="text/javascript"> //script脚本
function createload() {
var loaddiv = document.createElement("div");
loaddiv.style.width = "200px";
loaddiv.style.height = "100px";
loaddiv.style.border = "1px solid #000000";
loaddiv.style.fontSize = "12px";
loaddiv.style.lineHeight = "100px";
loaddiv.style.backgroundColor = "#cccccc";
loaddiv.style.textAlign = "center";
loaddiv.innerHTML = "请稍候...";
document.body.appendChild(loaddiv);
}
</script>
3、
for (int i = 0; i < 10; i++) // 单击生成按钮
{
DateTime startTime = DateTime.Now;
Response.Write(i + "-------------执行时间:" + startTime.ToString() + "<br />");
Thread.Sleep(1000); //Thread 线类 .sleep 暂定 (1000) 1秒钟
}
方法二、带进度条
1、创建bar.htm文件
<!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 language="javascript" type="text/javascript">
function $(obj) {
return document.getElementById(obj);
}
//i为当前执行到的记录,count为总数
//比如总共需要生成的静态数为100,那么现在执行到5%,那么进度条块进到5%的地方
function loadBar(i, count) {
var a = parseFloat(i * 100 / count);
$("bar").style.width = a + "%";
if ($("bar").style.width == "100%") {
$("bar").innerHTML = "完成";
} else {
$("bar").innerHTML = a + "%";
}
}
</script>
<style type="text/css">
body{ text-align:center; font-size:12px; }
.graph{ width:450px; border:1px solid #F8B3D0;height:25px; margin:0 auto; }
#bar{ display:block; background:#FFE7F4; float:left; height:100%;text-align:center; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="graph">
<strong id="bar" style="width:1%;"></strong>
</div>
</div>
</form>
</body>
</html>
2、单击按钮事件
for (int i = 0; i < 10; i++)
{
if (i == 0)
{
string strFileName = FileObj.ReadFile(Server.MapPath("bar.htm")); //调用FileObj文件操作类
Response.Write(strFileName);
}
Response.Write("<script>loadBar(" + (i + 1) + ",10);</script>");
Thread.Sleep(1000);
}
注:在使用该方法的.net文件中一定要在文件头定义Buffer="false"即关闭缓存
方法三、
try
{
DateTime dt = DateTime.Now;//当前时间
//生成静态
DateTime dt2 = DateTime.Now; //当前时间
TimeSpan ts = dt2.Subtract(dt); // 时间间隔
string strTs = ts.TotalSeconds.ToString();
ltGengerate.Text = "生成成功,生成用时" + strTs + "秒!"; //显示出来
}
catch (Exception ex)
{
throw ex;
}