25
2012
09

在ASP.NET中显示进度条(实用)

要使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页面,显示结果如下:

 20120925113907

« 上一篇下一篇 »

评论列表:

1.iphone4s手机壳  2012/9/25 17:51:26 回复该留言
好久没学net了
2.开心淘开心  2012/9/26 11:34:24 回复该留言
div id="ProgressBar" style="background-color:#3366FF; height:21px; width:0%;"
3.电动玩具  2012/9/26 11:35:22 回复该留言
StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("gb2312"));
4.寮步婚纱摄影  2012/11/19 22:11:51 回复该留言
嘻嘻,good im like
5.寮步婚纱摄影  2012/11/19 22:12:11 回复该留言
期待博主更新!@

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。