Asp.net基于ajax和jquery-ui實(shí)現(xiàn)進(jìn)度條
前臺(tái)用ajax不停進(jìn)行查詢,直到任務(wù)完成。進(jìn)度條用的是jquery-ui。后臺(tái)用一般處理程序處理相應(yīng),進(jìn)度信息保存在HttpContext.Application中。
代碼作為簡(jiǎn)單示例,實(shí)際應(yīng)用時(shí)應(yīng)對(duì)資源釋放、防止多線程混亂等做進(jìn)一步控制。
效果圖:
代碼:
前臺(tái):
<!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) { //工作沒有結(jié)束,繼續(xù)查詢進(jìn)度 setTimeout(GetProgress, 100); $("#progress").html(data); $("#progressbar").progressbar({ value: parseInt(data) }); } else { //工作完成 $("#progress").html("done"); $("#progressbar").progressbar({ value: 100 }); $("#btn1").attr("disabled", false); } } }); } function DoWork() { //禁用按鈕 $("#btn1").attr("disabled", true); $.ajax({ url: "/Handler1.ashx", type: "POST", data: { "RequestType": "AjaxRequest", "Method": "DoWork" } }); //開始查詢進(jìn)度 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>
后臺(tái):
// 2015年12月16日 09:53:11 // David Huang // 進(jìn)度條示例 namespace ProgressTest { using System; using System.Threading; using System.Web; /// <summary> /// Handler1 的摘要說明 /// </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++) { // 記錄進(jìn)度 // 實(shí)際應(yīng)用中需要進(jìn)一步控制(利用用戶信息、cookies等),防止并發(fā)造成混亂 this.context.Application["progress"] = i + 1; Random r = new Random(); Thread.Sleep(r.Next(10, 100)); } // 完成后釋放資源 this.context.Application["progress"] = null; } /// <summary> /// 查詢進(jìn)度 /// </summary> /// <returns>進(jìn)度</returns> private int GetProgress() { if (this.context.Application["progress"] != null) { return (int)this.context.Application["progress"]; } else { return -1; } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET實(shí)現(xiàn)進(jìn)度條效果
- asp.net mvc 實(shí)現(xiàn)文件上傳帶進(jìn)度條的思路與方法
- asp.net文件上傳帶進(jìn)度條實(shí)現(xiàn)案例(多種風(fēng)格)
- asp.net單文件帶進(jìn)度條上傳的解決方案
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- asp.net 生成靜態(tài)頁(yè)時(shí)的進(jìn)度條顯示
- 利用Asp.Net回調(diào)機(jī)制實(shí)現(xiàn)進(jìn)度條
- ASP.NET技巧:教你制做Web實(shí)時(shí)進(jìn)度條
- ASP.NET?MVC使用jQuery?ui的progressbar實(shí)現(xiàn)進(jìn)度條
相關(guān)文章
MVC5 + EF6 + Bootstrap3 (11) 實(shí)現(xiàn)排序、搜索、分頁(yè)
本篇文章主要介紹了MVC5 + EF6 + Bootstrap3 (11) 實(shí)現(xiàn)排序、搜索、分頁(yè),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-12-12手把手教你在.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法
這篇文章主要介紹了.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-12-12ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
ASP.NET MVC使用EasyUI的datagrid多選提交保存教程,需要的朋友可以參考下。2011-12-12.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc(續(xù))
怎樣直接獲取Ioc中的實(shí)例對(duì)象,而不是以構(gòu)造函數(shù)的方式進(jìn)行獲取呢?這篇文章繼續(xù)為大家介紹.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc2018-03-03asp.net treeview checkbox 相關(guān)問題
asp.net treeview checkbox 相關(guān)問題,需要的朋友可以看下。2009-06-06asp.net core 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的倉(cāng)儲(chǔ)的方法
本篇文章主要介紹了asp.net core 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的倉(cāng)儲(chǔ)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫(kù)權(quán)限管理
.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫(kù)權(quán)限管理 ,需要的朋友可以參考一下2013-02-02集合類List與Dictonary實(shí)例練習(xí)
本文將詳細(xì)介紹下List<>泛型集合/Dictonary<>字典/泛型集合練習(xí) /中日期轉(zhuǎn)換提取為方法以及泛型集合練習(xí)之翻譯軟件,感興趣的你可不要錯(cuò)過了哈2013-02-02