这篇文(wen)章主要介绍了Asp.net基于ajax和jquery-ui实现进度条,基(ji)于(yu)文中通过示例代码介绍的非常详细,对大家的现进学习或者工(gong)作具有(you)一定的参考学习价值,需要的朋友可以参考下
前台用ajax不停进行(xing)查询,直到任务完成。度条进度条用的基于是jquery-ui。后台用一般处理程序处理相应,现进进度信息保存在HttpContext.Application中。度条

代码作为(wei)简单示例,基于(yu)实际应用(yong)时应对资源释放(fang)、现进防止多线程混乱等做进(jin)一步控制。度条


代码:
前台:
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Scripts/jquery-2.1.4.min.js"></script>
<script src='Scripts/jquery-ui-1.11.4.min.js'></script>
<link href='Content/themes/base/all.css' rel="external nofollow" rel='stylesheet' />
<script type='text/javascript'>
function GetProgress() {
$.ajax({
url: "/Handler1.ashx",基于
type: "POST",
data: { "RequestType": "AjaxRequest", "Method": "GetProgress" },
success: function (data) {
if (data != -1) {
//工作没有结(jie)束,继续查询进度
setTimeout(GetProgress,现进 100);
$("#progress").html(data);
$("#progressbar").progressbar({ value: parseInt(data) });
} else {
//工作完(wan)成
$("#progress").html("done");
$("#progressbar").progressbar({ value: 100 });
$("#btn1").attr("disabled", false);
}
}
});
}
function DoWork() {
//禁用按钮
$("#btn1").attr("disabled="disabled"", true);
$.ajax({
url: "/Handler1.ashx",
type: "POST",
data: { "RequestType": "AjaxRequest", "Method": "DoWork" }
});
//开始查询进度
setTimeout(GetProgress, 500);
}
</script>
</head>
<body>
<div>
<input type='button' id='btn1' value="开始" onclick='DoWork();' />
<label id="progress"></label>
<div id="progressbar"></div>
</div>
</body>
</html>
后台:
// 2015年12月16日 09:53:11
// David Huang
// 进度条示例
namespace ProgressTest
{
using System;
using System.Threading;
using System.Web;
/// <summary>
/// Handler1 的(de)摘要说明(ming)
/// </summary>
public class Handler1 : IHttpHandler
{
// context
private HttpContext context;
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
this.context = context;
if (context.Request["RequestType"] == "AjaxRequest")
{
if (context.Request["Method"] == "GetProgress")
{
context.Response.Clear();
context.Response.Write(this.GetProgress());
context.Response.End();
}
else
{
this.DoWork();
}
}
}
/// <summary>
/// 开始工作
/// </summary>
private void DoWork()
{
for (int i = 0; i < 100; i++)
{
// 记录进度
// 实际(ji)应用中(zhong)需要进一步控制(利用用户信息、cookies等),度条防止并发造成混乱
this.context.Application["progress"] = i + 1;
Random r = new Random();
Thread.Sleep(r.Next(10,基于 100));
}
// 完成后释放资源
this.context.Application["progress"] = null;
}
/// <summary>
/// 查询进度
/// </summary>
/// <returns>进度</returns>
private int GetProgress()
{
if (this.context.Application["progress"] != null)
{
return (int)this.context.Application["progress"];
}
else
{
return -1;
}
}
}
}
以上(shang)就是本文的全部内容(rong),希望对大家的现进学习有所帮助,也希望大家多多支持脚本之家(jia)。度条
来源:脚本之(zhi)家
链接:https://www.jb51.net/article/201282.htm


网站二维码
导航
电话
短信
咨询
地图
分享