要使asp.net显示进度条和C#中类似,可以使用下面的方法:
添加一个静态页面:jingdu.html,源代码如下:
<!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 SetPorgressBar(pos) {
ProgressBar.style.width = pos + "%";
WriteText("Msg1", " 已完成" + pos + "%");
}
//设置进度条完成信息
function SetCompleted(msg)
{
if (msg == "")
WriteText("Msg1", "完成。");
else
WriteText("Msg1", 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>
<form id="form1" runat="server">
<div>
<div id="Msg1">
</div>
<div id="ProgressBarSide" style="color:Silver;border-width:1px;border-style:Solid;width:300px;">
<div id="ProgressBar" style="background-color:#3366FF; height:21px; width:0%;">
</div>
</div>
</div>
</form>
</body>
</html>
再添加一个asp.net页面:jingdu.aspx,只需要给他后台源代码:
protected void Page_Load(object sender, EventArgs e)
{
// 根据 ProgressBar.htm 显示进度条界面
string templateFileName = Path.Combine(Server.MapPath("."), "tongji.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(200);
// 根据处理任务处理情况更新进度条
string jsBlock;
for (int i = 1; i <= 100; i++)
{
System.Threading.Thread.Sleep(10);
jsBlock = "<script>SetPorgressBar(''" + i.ToString() + "'); </script>";
Response.Write(jsBlock);
Response.Flush();
}
// 处理完成
jsBlock = "<script>SetCompleted('处理完成。'); </script>";
Response.Write(jsBlock);
Response.Flush();
}
最后运行jingdu.aspx页面,显示结果如下:
评论列表: