用C#进的一个进度条

来源:互联网 发布:windows远程终端工具 编辑:程序博客网 时间:2024/06/11 19:07
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProgramBar.aspx.cs" Inherits="Example.Example.ProgramBar" %>

<!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>
</head>
<body>
    
<form id="form1" runat="server">
    
<script type="text/javascript">
       
//开始
      function BeginTrans(msg)
       
{
         ViewText(
"topMsg",msg); 
       }

       
//显示加载进度
       function ProgramBarInfo(msg,pos)
        
{
          programBar.style.width
= pos + "%";
         ViewText(
"topMsg",msg + " 已完成" + pos + "%");
        }
              
       
//设置加载时间
      function SetProgramBarTime(msg)
       
{
          ViewText(
"bottomMsg",msg);
       }

       
//加载结束
       function EndTrans(msg)
        
{
          
if(typeof(msg)=="undefined")
            ViewText(
"topMsg","完成");
           
else
            ViewText(
"topMsg",msg);       
            
        }

       
//显示文本 
       function ViewText(id,msg)
        
{
          var strTag 
= '<font face="Verdana, Arial, Helvetica" size="2" color="#ea9b02"><B>' + msg + '</B></font>';    
          
if (document.all) document.all[id].innerHTML = strTag;
        }

      
    
</script>
    
<div style="text-align:center">
    
<div id="topMsg" style="width:30%;">正在加载.......</div>
      
<div style="border:solid 1px #CCC;width:30%; text-align:left" id="ProgramSide">
        
<div id="programBar" style="height:30px; background-color:#316AC5;width:0px;"></div>
      
</div>
      
<div id="bottomMsg" style="width:30%;"></div>
    
</div>
    
<div>
      
<%=response %>
    
</div>
    
</form>
    
    
    
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Example.Example
{
    
public partial class ProgramBar : System.Web.UI.Page
    
{
        
public static string response;
        
protected void Page_Load(object sender, EventArgs e)
        
{
            
if (!IsPostBack)
            
{
                DateTime startTime 
= DateTime.Now;
                DateTime endTime 
= DateTime.Now;
                
string jsBlock;
                
// 处理完成
                response += "<script type="text/javascript">BeginTrans('开始处理...');</script> ";
                
// Response.Write(jsBlock);
                System.Threading.Thread.Sleep(1000);

                
// 根据处理任务处理情况更新进度条
                for (int i = 1; i <= 100; i++)
                
{
                    System.Threading.Thread.Sleep(
20);
                    response 
+= "<script type="text/javascript">ProgramBarInfo('" + "A" + i.ToString() + "','" + i.ToString() + "');</script> ";
                    
//Response.Write(jsBlock);
                }


                
// 处理完成
                response += "<script type="text/javascript">EndTrans('处理完成。');</script> ";
                
//Response.Write(jsBlock);

                
// 用时
                endTime = DateTime.Now;
                response 
+= "<script type="text/javascript">SetProgramBarTime('用时" + GetTimeSpan(startTime, endTime) + "');</script>";
                
//Response.Write(jsBlock);
            }

           
        }


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

    }

}