`

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实时进度条

    网上已经有很多Web进度条的例子,但是很多都是估算时间,不能正真反应任务的真实进度。我自己结合多线程和ShowModalDialog制做了一个实时进度条,原理很简单:使用线程开始长时间的任务,定义一个Session,当任务...

    ASP.NET2.0 WEB实时进度条

    在本文中,我们将深入探讨如何利用ASP.NET 2.0实现一个实时进度条,为用户提供更好的交互体验,提升网页的视觉效果。 首先,让我们了解什么是实时进度条。实时进度条是一种用户界面元素,它可以显示某个任务或操作...

    asp.net 文件上传 实时进度

    您可能感兴趣的文章:ASP.NET技巧:教你制做Web实时进度条asp.net下大文件上传知识整理ASP.NET实现用图片进度条显示投票结果asp.net 大文件上传控件asp.net 文件上传与刷新与asp.net页面与iframe之间的数据传输asp...

    web网页ASP.NET进度条编写

    web,网页,ASP.NET,进度条,编写 web,网页,ASP.NET,进度条,编写 web,网页,ASP.NET,进度条,编写 web,网页,ASP.NET,进度条,编写 web,网页,ASP.NET,进度条,编写 web,网页,ASP.NET,进度条,编写 ...

    asp.net 各种进度条

    ASP.NET 是一种强大的Web应用程序开发框架,由微软公司提供,用于构建动态网站、Web应用程序和服务。在【标题】"asp.net 各种进度条"中,我们关注的是在ASP.NET环境中实现进度条功能的各种方法。进度条是用户界面中...

    asp.net实现进度条

    ASP.NET 是一种强大的 Web 应用程序开发框架,由微软公司提供,用于构建动态、数据驱动的 Web 应用程序。在 ASP.NET 中实现进度条功能可以极大地提升用户体验,尤其是在处理长时间运行的任务时,如文件上传、大数据...

    【ASP.NET编程知识】asp.net单文件带进度条上传的解决方案.docx

    ASP.NET单文件带进度条上传解决方案 本文主要讲述了ASP.NET单文件带进度条上传的解决方案,通过使用jquery框架和ASP.NET缓存机制,实现了文件上传和进度条显示的功能。 知识点1:使用jquery框架实现文件上传 在...

    asp.net前台显示后台处理进度条

    1. 创建ASP.NET页面:首先创建一个新的ASP.NET Web Forms页面,添加必要的控件,例如一个UpdateProgress控件用于显示进度条,一个Button控件触发后台处理,以及一个Label或任何其他控件用于显示进度信息。...

    Asp.net_WEB 版 WebUploader大文件上传带进度条

    【ASP.NET Web版 WebUploader大文件上传带进度条】是一个针对ASP.NET 4.0 Web应用程序的文件上传解决方案,特别适合处理大文件并提供实时的上传进度反馈。WebUploader是一款前端JavaScript库,它允许用户在浏览器端...

    asp.net上传图片带进度条

    在ASP.NET中实现图片上传并显示进度条的功能是一项常见的需求,尤其在用户上传大体积图片时,进度条能提供良好的用户体验。以下将详细介绍如何在ASP.NET中构建这样的功能。 首先,我们需要理解上传过程的基本原理。...

    asp.net 超大文件上传,带进度条源码,亲测能用

    ASP.NET 是微软开发的一种用于构建Web应用程序的框架,它提供了丰富的功能和强大的性能。在处理大文件上传时,ASP.NET需要特别的处理,因为默认情况下,它对上传文件的大小有限制,通常为4MB左右。针对超大文件上传...

    ASP.NET实现EXCEL数据导入进度条ajax

    本文将深入探讨如何使用ASP.NET结合Ajax技术实现Excel数据导入时的进度条显示。 首先,我们需要理解ASP.NET的页面生命周期和Ajax的工作原理。ASP.NET是一个服务器端的Web应用程序框架,它负责处理用户的请求、执行...

    Asp.net实现进度条完整功能(附源码)

    在Asp.net开发中,有时候我们需要为用户呈现一个可视化的进度条来表示某个长时间运行的操作的进度,例如文件上传、数据处理等。本教程将详细讲解如何在Asp.net中实现一个具有完整功能的进度条,并提供源码供参考。 ...

    ASP.NET 带进度条上传 DEMO

    ASP.NET 是微软开发的一种用于构建Web应用程序的框架,它提供了丰富的功能和强大的工具来帮助开发者构建动态、数据驱动的网站。在ASP.NET中处理文件上传是一项常见的任务,尤其是在需要用户提交大文件时,如照片、...

    一个基于ASP.NET+C#实现的Web上传进度条控件程序例子

    "一个基于ASP.NET+C#实现的Web上传进度条控件程序例子"就是为了满足这一需求而设计的。它允许用户在上传文件过程中看到实时的进度,提升用户体验并减少用户的焦虑感。 首先,我们要理解什么是“进度条控件”。在UI...

    asp.net+sql2000加载数据真实进度条

    在本项目中,"asp.net+sql2000加载数据真实进度条"是一个实现用户交互和后台数据处理的实例,它展示了如何在用户界面中提供实时的数据加载进度反馈,以提升用户体验。 在ASP.NET中,我们通常会用到AJAX技术来实现...

    采用asp.net+ajax技术实现的带进度条上传用户控件源码

    这个“采用asp.net+ajax技术实现的带进度条上传用户控件源码”是一个很好的实例,展示了如何利用这两种技术来创建一个具有实时反馈的文件上传功能。 ASP.NET是微软提供的一个强大的Web应用程序框架,它允许开发者...

    asp.net文件上传带进度条

    在ASP.NET中实现文件上传带进度条的功能,通常涉及到AJAX技术,以便提供更好的用户体验。在传统的文件上传中,用户可能会遇到页面刷新或者长时间无响应的情况,而通过AJAX技术,我们可以实现异步文件上传,同时展示...

    asp.net页面加载前进度条

    在ASP.NET Web应用程序开发中,有时为了提升用户体验,在页面加载过程中显示一个进度条或加载动画是常见的做法。本文将详细介绍如何在ASP.NET应用中实现页面加载前进度条的功能。 #### 一、背景与需求分析 随着Web...

Global site tag (gtag.js) - Google Analytics