一个asp.net可以用的进度条
首先声明不是自己写的,是从网上发现的
第一步先写个静态页progressbar.htm
<script language="javascript">
function setPgb(pgbID, pgbValue)
{
if ( pgbValue <= 100 )
{
if (lblObj = document.getElementById(pgbID + "_label"))
{
lblObj.innerHTML = pgbValue + "%"; // change the label value
}
if ( pgbObj = document.getElementById(pgbID) )
{
var divChild = pgbObj.children[0];
pgbObj.children[0].style.width = pgbValue + "%";
}
}
}
</script>
<html>
<head>
<style type="text/css">
.bi-loading-status {
width: 100%
padding: 1px;
overflow: hidden;
}
.bi-loading-status .text {
white-space: nowrap;
overflow: hidden;
width: 10%;
text-overflow: ellipsis;
padding: 1px;
float: right;
}
.bi-loading-status .progress-bar {
border: 1px solid ThreeDShadow;
background: window;
height: 16px;
width: 88%;
padding: 1px;
overflow: hidden;
float: left;
}
.bi-loading-status .progress-bar div {
background: Highlight;
overflow: hidden;
height: 100%;
filter: Alpha(Opacity=0, FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=0);
}
</style>
</head>
<body topmargin="0" leftmargin="0" bottommargin="0" rightmargin="0">
<table border="0" align="center" width="70%"><tr><td>
<DIV class="bi-loading-status" id="proBar" style="width: 100%;">
<DIV class="text" id="pgbMain_label" align="left"></DIV>
<DIV class="progress-bar" id="pgbMain" align="left">
<DIV STYLE="WIDTH:10%"></DIV>
</DIV>
</DIV>
</td></tr></table>
</body>
</html>
function setPgb(pgbID, pgbValue)
{
if ( pgbValue <= 100 )
{
if (lblObj = document.getElementById(pgbID + "_label"))
{
lblObj.innerHTML = pgbValue + "%"; // change the label value
}
if ( pgbObj = document.getElementById(pgbID) )
{
var divChild = pgbObj.children[0];
pgbObj.children[0].style.width = pgbValue + "%";
}
}
}
</script>
<html>
<head>
<style type="text/css">
.bi-loading-status {
width: 100%
padding: 1px;
overflow: hidden;
}
.bi-loading-status .text {
white-space: nowrap;
overflow: hidden;
width: 10%;
text-overflow: ellipsis;
padding: 1px;
float: right;
}
.bi-loading-status .progress-bar {
border: 1px solid ThreeDShadow;
background: window;
height: 16px;
width: 88%;
padding: 1px;
overflow: hidden;
float: left;
}
.bi-loading-status .progress-bar div {
background: Highlight;
overflow: hidden;
height: 100%;
filter: Alpha(Opacity=0, FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=0);
}
</style>
</head>
<body topmargin="0" leftmargin="0" bottommargin="0" rightmargin="0">
<table border="0" align="center" width="70%"><tr><td>
<DIV class="bi-loading-status" id="proBar" style="width: 100%;">
<DIV class="text" id="pgbMain_label" align="left"></DIV>
<DIV class="progress-bar" id="pgbMain" align="left">
<DIV STYLE="WIDTH:10%"></DIV>
</DIV>
</DIV>
</td></tr></table>
</body>
</html>
第二步 在cs文件里写个方法
protected void processBar(int c, int t)
{
if (c == 0)
{
string strFileName = Request.PhysicalApplicationPath + "\\progressbar.htm";
System.IO.StreamReader sr = new System.IO.StreamReader(strFileName, System.Text.Encoding.Default);
string strHtml = sr.ReadToEnd();
Response.Write(strHtml);
sr.Close();
Response.Flush();
}
Response.Write("<script>setPgb('pgbMain'," + ((c + 1) * 100) / t + ");</script>");
Response.Flush();
}
{
if (c == 0)
{
string strFileName = Request.PhysicalApplicationPath + "\\progressbar.htm";
System.IO.StreamReader sr = new System.IO.StreamReader(strFileName, System.Text.Encoding.Default);
string strHtml = sr.ReadToEnd();
Response.Write(strHtml);
sr.Close();
Response.Flush();
}
Response.Write("<script>setPgb('pgbMain'," + ((c + 1) * 100) / t + ");</script>");
Response.Flush();
}
参数说明 c是当前执行次数,t是总次数
调用方法如:
int total=100;//
for (int i = 0; i < total; i++)
{
//这里你想做什么就做吧
processBar(i, total);
}
for (int i = 0; i < total; i++)
{
//这里你想做什么就做吧
processBar(i, total);
}