.net代碼 利用Ajax+Jquery實現異步進度條效果

Index.aspx前臺頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="TestAjaxProgressbar.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="Script/jquery-1.8.2.js"></script>
    <link href="Script/jquery-ui.min.css" rel="stylesheet" />
    <script src="Script/jquery-ui.min.js"></script>
    <title>測試Ajax進度條</title>
    <script type="text/javascript">
        function GetProgress() {
            $.ajax({
                url: "/Processing.ashx",
                type: "POST",
                data: { "RequestType": "AjaxRequest", "Method": "GetProgress" },
                success: function (data) {
                    if (data != -1) {
                        //工作沒有結束,繼續查詢進度
                        setTimeout(GetProgress, 500);                       
                        $("#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: "/Processing.ashx",
                type: "POST",
                data: { "RequestType": "AjaxRequest", "Method": "DoWork" }
            });
            //開始查詢進度
            setTimeout(GetProgress, 500);
        }


 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="btn1" value="開始" onclick="DoWork();" /> 
       
        <label id="progress"></label>
        
        <div id="progressbar"  class="easyui-progressbar" style="width:500px;height:35px"></div>
    </div>
    </form>
</body>
</html>

Processing.ashx後臺

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace TestAjaxProgressbar
{
    /// <summary>
    /// Processing 的摘要說明
    /// </summary>
    public class Processing : 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++)
                {
                    // 記錄進度
                    // 實際應用中需要進一步控制(利用用戶信息、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;
                }
            }
        }
}

源碼下載 https://download.csdn.net/download/hq0927/11217037

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章