ASP.NET技巧:教你制做Web实时进度条
网上已经有很多Web进度条的例子,但是很多都是估算时间,不能正真反应任务的真实进度。我自己结合多线程和ShowModalDialog制做了 一个实时进度条,原理很简单:使用线程开始长时间的任务,定义一个Session,当任务进行到不同的阶段改变Session的值,线程开始的同时使用 ShowModalDialog打开一个进度条窗口,不断刷新这个窗口获取Session值,反应出实时的进度。下面就来看看具体的代码:(文章结尾处下 载源代码)
先新建一个Default.aspx页面,
客户端代码:
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<br>
<br>
<asp:Button id="Button1" runat="server" Text="Start Long Task!"></asp:Button>
</form>
</body>
服务器端代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace WebProgressBar
{
/**//// <summary>
/// Summary description for _Default.
/// </summary>
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LongTask()
{
//模拟长时间任务
//每个循环模拟任务进行到不同的阶段
for(int i=0;i<11;i++)
{
System.Threading.Thread.Sleep(1000);
//设置每个阶段的state值,用来显示当前的进度
Session["State"] = i+1;
}
//任务结束
Session["State"] = 100;
}
public static void OpenProgressBar(System.Web.UI.Page Page)
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
//需要IE5.5以上支持
sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight:
100px; dialogWidth: 350px; edge: Raised; center: Yes; help: No;
resizable: No; status: No;scroll:No;');\n");
//IE5.5以下使用window.open
//sbScript.Append("window.open('Progress.aspx','', 'height=100,
width=350, toolbar =no, menubar=no, scrollbars=no, resizable=no,
location=no, status=no');\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");
Page.RegisterClientScriptBlock("OpenProgressBar", sbScript.ToString());
}
private void Button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(LongTask));
thread.Start();
Session["State"]=1;
OpenProgressBar(this.Page);
}
}
}
新建一个进度条页面Progress.aspx
客户端:
在head中加入<base target="_self">
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblMessages" runat="server"></asp:Label>
<asp:Panel id="panelBarSide" runat="server" Width="300px" BorderStyle="Solid" BorderWidth="1px"
ForeColor="Silver">
<asp:Panel id="panelProgress" runat="server" Width="10px" BackColor="Green"></asp:Panel>
</asp:Panel>
<asp:Label id="lblPercent" runat="server" ForeColor="Blue"></asp:Label>
</form>
</body>
服务器端:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebProgressBar
{
/**//// <summary>
/// Summary description for Progress.
/// </summary>
public class Progress : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessages;
protected System.Web.UI.WebControls.Panel panelProgress;
protected System.Web.UI.WebControls.Panel panelBarSide;
protected System.Web.UI.WebControls.Label lblPercent;
private int state = 0;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(Session["State"]!=null)
{
state = Convert.ToInt32(Session["State"].ToString());
}
else
{
Session["State"]=0;
}
if(state>0&&state<=10)
{
this.lblMessages.Text = "Task undertaking!";
this.panelProgress.Width = state*30;
this.lblPercent.Text = state*10 + "%";
Page.RegisterStartupScript("","<script>window.setTimeout('window.Form1.submit()',100);</script>");
}
if(state==100)
{
this.panelProgress.Visible = false;
this.panelBarSide.Visible = false;
this.lblMessages.Text = "Task Completed!";
Page.RegisterStartupScript("","<script>window.close();</script>");
}
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
ASP.NET技巧:教你制做Web实时进度条的更多相关文章
- Java web实时进度条整个系统共用(如java上传进度条、导入excel进度条等)
先上图: 这上文件上传的: 这是数据实时处理的: 1:先说说什么是进度条:进度条即计算机在处理任务时,实时的,以图片形式显示处理任务的速度,完成度,剩余未完成任务量的大小,和可能需要处理时间,显示方式 ...
- Java web实时进度条整个系统共用(如java上传、下载进度条、导入、导出excel进度条等)
先上图: 文件上传的: 2017-05-04再次改进.在上传过程中用户可以按 Esc 来取消上传(取消当前上传,或者是全部上传)... 2019-03-26更新进度条显示体验 从服务器上压缩下载: 从 ...
- ASP.NET中二进制流下载文件时进度条的使用
说明 在下载大文件时,页面会进入假死状态,于是加上一个进度条以标识后台程序正在运行. 目前,做的进度条并不是实时的,并不会根据程序执行的进度正确显示. 目前是将进度条定时加载到90%,然后停止,等待后 ...
- 居于Web的进度条实现思路(下载百分比)
http://www.cnblogs.com/wfyfngu/p/4866434.html 在传统桌面项目中,进度条随处可见,但作为一个很好的用户体验,却没有在如今主流的B/S程序中得到传承,不能不说 ...
- asp.net 后台多线程异步处理时的 进度条实现一(Ajax+Ashx实现以及封装成控件的实现)
(更新:有的同学说源代码不想看,说明也不想看,只想要一个demo,这边提供一下:http://url.cn/LPT50k (密码:TPHU)) 工作好长时间了,这期间许多功能也写成了不少的控件来使用, ...
- ASP.NET Jquery+ajax上传文件(带进度条)
效果图 支持ie6+,chrome,ie6中文文件名会显示乱码. 上传时候会显示进度条. 需要jquery.uploadify.js插件,稍后会给出下载 前台代码 <%@ Page Langua ...
- asp.net mvc 实现上传文件带进度条
本文乃是博主早期写的,此种思路虽然实现了,但固然不是最好的,仅做参考学习. 可以用js onprogress .fileinput .webuploader.jq ajaxsubmit等实现 思路:a ...
- Asp.net mvc 大文件上传 断点续传 进度条
概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这篇文章,此方法确实很不错,能够稳定的上传大文件,http: ...
- 使用 Asp.Net Response.Write() 制作实时进度条
准备: 一个 StudyResponse.aspx 页面和 CodeBehind 文件. Web 页面中的内容如下: <%@ Page Language="C#" AutoE ...
随机推荐
- 基于Oracle的Mybatis 批量插入
项目中会遇到这样的情况,一次性要插入多条数据到数据库中,有两种插入方法: 方法一: Mybatis本身只支持逐条插入,比较笨的方法,就是遍历一个List,循环中逐条插入,比如下面这段代码 for(Da ...
- js-d3画图插件
d3.js下载 官方网站:http://d3js.org/ github:https://github. com/mbostock/d3/tags forks最新:https://github.com ...
- 查看mysql语句运行时间
show profiles 之类的语句来查看 mysql> show profiles; Empty set mysql> show variables like "%pro%& ...
- C语言中数组名作为参数进行函数传递
用数组名作函数参数与用数组元素作实参有几点不同. 1) 用数组元素作实参时,只要数组类型和函数的形参变量的类型一致,那么作为下标变量的数组元素的类型也和函数形参变量的类型是一致的.因此,并不要求函数的 ...
- 回合对战制游戏第一篇(初识java)
回合对战制游戏第一篇 一,所谓的java. java是一门完全面向对象的编程语言,而之前所接触到的C语言是一门面向有一个过程的语音,对于两个的区别应该有一个清楚的认识. java的第一个内容. 类和对 ...
- bzoj 1257
商最多有sqrt(n)个. #include<iostream> #include<cstdio> #include<cstring> #include<al ...
- 【BZOJ-4289】Tax 最短路 + 技巧建图
4289: PA2012 Tax Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 168 Solved: 69[Submit][Status][Dis ...
- 【BZOJ-2839】集合计数 容斥原理 + 线性推逆元 + 排列组合
2839: 集合计数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 229 Solved: 120[Submit][Status][Discuss] ...
- 【BZOJ-1497】最大获利 最大流
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3800 Solved: 1848[Submit][Status] ...
- jdbc工具类封装
封装 package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...