C#asp.net 进度条 测试

using System;
using System.Collections.Generic;

C#asp.net_进度条_测试



using System.Linq;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        beginProgress();
        for (int i = 1; i <= 100; i++)
        {
            setProgress(i);
            //此处用线程休眠代替实际的操作,如加载数据等   
            System.Threading.Thread.Sleep(10);
        }

        finishProgress();
    }




    private void beginProgress()
    {
        //根据ProgressBar.htm显示进度条界面   
        string templateFileName = System.IO.Path.Combine(Server.MapPath("."), "ProgressBar.htm");
        System.IO.StreamReader reader = new System.IO.StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
        string html = reader.ReadToEnd();
        reader.Close();
        Response.Write(html);
        Response.Flush();
    }

    private void setProgress(int percent)
    {
        string jsBlock = " ";
        Response.Write(jsBlock);
        Response.Flush();
    }

    private void finishProgress()
    {
        string jsBlock = " ";
        Response.Write(jsBlock);
        Response.Flush();
    }





还需要制作一个 ProgressBar.htm




<script language="javascript">
    function SetPorgressBar(pos) {
        //设置进度条居中

        var screenWidth = document.body.offsetWidth;
        ProgressBarSide.style.width = Math.round(screenWidth / 2) + "px";
        ProgressBarSide.style.left = Math.round(screenWidth / 4) + "px";
        ProgressBarSide.style.top = "50px";
        ProgressBarSide.style.height = "21px";
        ProgressBarSide.style.display = "block";

        //设置进度条百分比 
        ProgressBar.style.width = pos + "%";
        ProgressText.innerHTML = pos + "%";
    }

    function SetMaxValue(maxValue) {
        ProgressBarSide.style.width = maxValue + "px";
    }

    //完成后隐藏进度条
    function SetCompleted() {
        ProgressBarSide.style.display = "none";
    }

    function SetTitle(title) {
        ProgressTitle.innerHTML = title;
    }
</script>
<div id="ProgressBarSide" style="position: absolute; height: 21px; width: 100px;
    color: Silver; border-width: 1px; border-style: Solid; display: block">
    <div id="ProgressBar" style="position: absolute; height: 21px; width: 0%; background-color: #1475BB">
    </div>
    <div id="ProgressText" style="position: absolute; height: 21px; width: 100%; text-align: center">
    </div>
    <div id="ProgressTitle" style="position: absolute; height: 21px; top: 21px; width: 100%;
        text-align: center">
    </div>
</div>



webform
桂ICP备16004754号