Asp. NET skill: teach you to make the Web real Time progress bar _ practical Skill

Source: Internet
Author: User

There are many examples of web progress bars on the internet, but many are estimates of time and cannot really reflect the real progress of a task. I have made a real-time progress bar by combining multithreading and showmodaldialog, the principle is simple: Use the thread to start a long task, define a session, when the task to different stages to change the value of sessions, The thread starts by using ShowModalDialog to open a progress bar window, constantly refreshing the window to get the session value, reflecting the real-time progress. Here is a look at the specific code: (Download the source code at the end of the article)

First create a new Default.aspx page,
Client code:

<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>
Server-side code:
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 the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Required to Designer support-do not modify
The contents is 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 ()
{
Simulate long time tasks
Each cycle simulates a task to a different stage
for (int i=0;i<11;i++)
{
System.Threading.Thread.Sleep (1000);
Sets the state value for each stage to display the current progress
session["state"] = i+1;
}
Mission over.
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");
Need IE5.5 above support
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 Use the following 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 ("//-->\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);
}
}
}


Create a new progress bar page progress.aspx
Client:
Add <base target= "_self" to head >
<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>
Server side:
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 () ',;</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 the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.