教你制做Web实时进度条

2016-02-19 16:08 33 1 收藏

下面图老师小编跟大家分享教你制做Web实时进度条,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

【 tulaoshi.com - 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;i11;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'");
              sbScript.Append("!--");
              //需要IE5.5以上支持
              sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight: 100px; dialogWidth: 350px; edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No;');");
              //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');");
              sbScript.Append("// --");
              sbScript.Append("/script");

              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);
          }
      }
  }

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

  
  新建一个进度条页面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;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

  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(state0&&state=10)
              {
                  this.lblMessages.Text = "Task undertaking!";
                  this.panelProgress.Width = state*30;
                  this.lblPercent.Text = state*10 + "%";
                  Page.RegisterStartupScript("","scriptwindow.setTimeout('window.Form1.submit()',100);/script");
              }
              if(state==100)
              {
                  this.panelProgress.Visible = false;
                  this.panelBarSide.Visible = false;
                  this.lblMessages.Text = "Task Completed!";
                  Page.RegisterStartupScript("","scriptwindow.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
      }
  }
  源代码下载:WebProgressBar.rar

  http://justicfu.cnblogs.com/archive/2006/06/29/438433.html

来源:http://www.tulaoshi.com/n/20160219/1611116.html

延伸阅读
标签: Web开发
CSS:  程序代码 style #graphbox{ border:1px solid #e7e7e7; padding:10px; width:250px; background-color:#f8f8f8; margin:5px 0; } #graphbox h2{ color:#666666; font-family:Arial; font-size:18px; font-weight:700; } .graph{ position:relative; background-color:#F0EFEF; border:1px solid #cccccc; padding:2px; font-size:1...
标签: Web开发
1.建立进度条html页面 progressbar.htm script language="javascript" function setPgb(pgbID, pgbValue) { if ( pgbValue = 100 ) { //debugger; if (lblObj = document.getElementById(pgbID+'_label')) { lblObj.innerHTML = pgbValue + '%'; // change the label value } if ( pgbObj = document.getElementBy...
新建一个工程 增加一个picture box和command button 加入下面的代码:Dim tenth As Long'条件编译#If Win32 ThenPrivate Declare Function BitBlt Lib "gdi32" _(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _ByVal nWidth As Long, ByVal nHeight As Long, _ByVal hSrcDC As Long, ByVal xSrc As...
标签: FLASH flash教程
本文由 txwh2006(踏雪无痕)   原创,转载请保留此信息! 这几天为了制作网页,需要做个进度条,无奈以前不注意,至今还不会做,只得看网上教程来学习。网上教程虽然不少,但其实版本雷同,真要学还不如意,有的教程号称“最简单”,他给你一段代码,叫你哭笑不得,人家会的高手,他也不在乎你一段代码,菜鸟们看了也不懂;有的教程...
《无尽的进度条Progress Quest》部分法术效果攻略 提示1:《无尽的进度条》 为半即时回合制游戏,即那种战斗开始后敌我都有行动条,走到头才能行动的回合制。 瞬移:有一定几率让你无视速度,立刻行动,几率随法术等级上升。 兔子的正义铁拳:召...

经验教程

565

收藏

55
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部