杨应红的WEB技术文档
WEB相关技术
第一步:




       第一步让我们来建立一个进度条页面,我们使用progressbar.aspx。如上所述,我们逐步生成并发送页面到客户端。首先,我们将使用HTML生成一个完美并且漂亮的进度条。所需要的HTML代码我们可以从事先定义的progressbar.htm中获取,所以,第一件事情是装载它并且将数据流发送到客户端的浏览器,在progressbar.aspx的Page_Load中加入如下几行:



            string strFileName = Path.Combine( Server.MapPath("./include"), "progressbar.htm" );



         StreamReader sr = new StreamReader( strFileName, System.Text.Encoding.Default );



         string strHtml = sr.ReadToEnd();



         Response.Write( strHtml );



         sr.Close();



         Response.Flush();



     以下是progressbar.htm的HTML代码,(译注:作者把脚本函数放在了另外一个js文件中,但我嫌麻烦就也放在了这个静态页面中了)



<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 + "%";



         }



         window.status = "数据读取" + pgbValue + "%,请稍候...";



     }



    



     if ( pgbValue == 100 )



         window.status = "数据读取已经完成";



 



}



</script>



<html>



     <head>



         <link rel="stylesheet" type="text/css" href="style/common.css" />



     </head>



     <body bgColor="buttonface" topmargin="0" leftmargin="0">



          <table width="100%" height="100%" ID="Table1">



              <tr>



                   <td align="center" valign="middle">



                       <DIV class="bi-loading-status" id="proBar" style="DISPLAY: ; LEFT: 425px; TOP: 278px">



                            <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>



对应的CSS定义如下:



.bi-loading-status {



  /*position:   absolute;*/



  width:        150px;



  padding: 1px;



  overflow: hidden; 



}







.bi-loading-status .text {



  white-space:  nowrap;



  overflow:     hidden;



  width:             100%;



  text-overflow:     ellipsis;



  padding:      1px;



}







.bi-loading-status .progress-bar {



  border:       1px solid ThreeDShadow;



  background:   window;



  height:       10px;



  width:        100%;



  padding: 1px;



  overflow: hidden;



 



}







.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);



}



第二步:




现在我们可以开始更新进度条了,这是十分重要的部分,因为这部分可以令你的进度条活起来。(译注,原来是使用随机的增加15次直到加载完毕,本文仅使用从1-100的匀速增加,以下内容非译)

首先我们新开一个线程,在Page_Load   中加入以下代码:



         Thread thread = new Thread( new ThreadStart(ThreadProc) );



         thread.Start();



     thread.Join();



在类中增加以下函数:



         private void ThreadProc()



         {



              string strScript = "<script>setPgb('pgbMain','{0}');</script>";



              for ( int i = 0; i <= 100; i++ )



              {



                   System.Threading.Thread.Sleep(10);



                   Response.Write( string.Format( strScript, i ) );



                   Response.Flush();



              }



     }



注意,在以上代码中我们使用for循环,在i每增加一次的情况下往客户端发送一段脚本<script>setPgb('pgbMain','{0}');</script>,其中的{0}会被相应的i替换,而该段脚本会调用预先写好的javascript函数setPgb,更改页面的进度条状态。
posted on 2006-11-27 15:19  落尘  阅读(201)  评论(0编辑  收藏  举报