天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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("http:// -->/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
    }
}

 

AspNet技術ASP.NET技巧:教你制做Web實時進度條,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: xxx日本免费| 亚洲精品午夜久久久伊人 | 国产日韩亚洲精品视频 | 好硬好湿好爽再深一点视频 | 国产在线精品视频免费观看 | 欧洲另类一二三四区 | 国产精品久久久久久AV免费不卡 | 摥管专用动态图399期 | 丰满五十六十老熟女HD60 | 麻豆精品人妻一区二区三区蜜桃 | 牢记永久免费网址 | 亚洲 欧美 中文 日韩 视频 | 亚洲欧美日韩高清专区 | 欧美一区二区视频在线观看 | 欧洲老妇人bb | 女人十八毛片水真多啊 | 91免费精品国自产拍在线可以看 | 亚洲AV福利天堂一区二区三 | 同时和两老师双飞 | 一本道高清码v京东热 | 女教师跟黑人男朋友激情过后 | 扒开美女下面粉嫩粉嫩冒白浆 | 欧美亚洲日韩国产在线在线 | 美国大臿蕉香蕉大视频 | 黑色丝袜美腿美女被躁翻了 | 国产区精品综合在线 | 国产免费人成在线视频有码 | 99久久爱看免费观看 | 旧里番YY6080在线播放 | 日本美女阴道 | 精品一区二区三区免费观看 | 稚嫩挤奶h调教h | 哪里能看毛片 | 亚洲 欧美 中文 日韩 视频 | 亚洲 欧美 中文 日韩 另类 | 亚洲欧美强伦一区二区另类 | 乳巨揉みま痴汉电车中文字幕动漫 | 午理论理影片被窝 | 国产亚洲美女精品久久久2020 | 理论片午午伦夜理片影院 | 亚洲女人毛片 |