團隊!!
我有一個網站,可以讓人們將資料下載到一個excel或pdf檔案中。下載需要足夠長的時間,我想在處理程序中顯示一個 "請等待 "的模式。 我發現一些代碼可以做到這一點,但問題是它只是從1數到100,然后就退出了。我想改變它,使其顯示到下載完成為止。
當用戶點擊按鈕開始匯出時,expot_click 函式被呼叫。 它下面的WorkerMethod函式被傳遞到類中,并在這里發生了從1到100的計數。我不確定該怎么改,或者在哪里改。
protected void export_click(object sender, EventArgs e)。
{
string exportType = hidExportType.Value;
string exportSections = hidSections.Value;
string exportStudents = hidStudents.Value;
//Display a "Please Wait" message while the download is happening。
object result = WaitWindow.Show(this.WorkerMethod) 。
switch (exportType)
{
case "csv"。
export_csv(exportSections, exportStudents)。
break;
case "excel"/span>:
export_excel(exportSections, exportStudents)。
break;
case "pdf"/span>:
export_pdf(exportSections, exportStudents)。
break;
default:
break;
}
//I'm guessing here is where I'd want to do something to make the wait display quit?
}
private void WorkerMethod(object sender, Jacksonsoft. WaitWindowEventArgs e)。
{
//做一些事情。
for (int progress = 1; progress <= 100; progress )
{
System.Threading.Thread.Sleep(20)。
//更新等待視窗的資訊。
e.Window.Message = string.Format
("Please wait ... {0}%", progress.ToString().PadLeft(3) )。
}
//使用發送的引數。
if (e.Arguments.Count > 0)
{
//設定要回傳的結果。
e.Result = e.Arguments[0].ToString()。
}
else[/span
{
//設定要回傳的結果。
e.Result = "Hello World"。
}
}
下面是被呼叫的類:
/*。
* 由SharpDevelop創建。
* 用戶:mjackson
* 日期:05/03/2010
* 時間:09:36
*
* 要改變這個模板,請使用工具|選項|編碼|編輯標準頭檔案。
*/
使用系統。
使用System.Collections.Generic;
使用System.Windows.Forms.Console.Generic; 使用System.Windows.Forms.Console.Generic;
namespace Jacksonsoft
{
// <summary>
// 顯示一個視窗,告訴用戶在一個行程執行時要等待。
// </summary>/span>
public class WaitWindow[/span
{
// <summary>
// 在執行所傳遞的方法時,顯示一個等待視窗,文本為 "請等待..."。
// </summary>/span>
// <param name="workerMethod">指標指向顯示等待視窗時要執行的方法。</param>
// <returns>來自worker方法的結果引數。</returns>
public static object Show(EventHandler<。 WaitWindowEventArgs> workerMethod){
return WaitWindow.Show(workerMethod, null)。
}
// <summary>
// 在執行通過的方法時顯示一個帶有指定文本的等待視窗。
// </summary>/span>
// <param name="workerMethod">/span>指向顯示等待視窗時執行的方法的指標。</param>
// <param name="message">/span>要顯示的文本。</param>
// <returns>作業者方法的結果引數。</returns>
public static object Show(EventHandler<。 WaitWindowEventArgs> workerMethod, string message) {
WaitWindow instance = new WaitWindow()。
return instance.Show(workerMethod, message, new List<object>())。
}
// <summary>
// 在執行通過的方法時,顯示一個帶有指定文本的等待視窗。
// </summary>/span>
// <param name="workerMethod">/span>指向顯示等待視窗時執行的方法的指標。</param>
// <param name="message">/span>要顯示的文本。</param>
// <param name="args">要傳遞給worker方法的引數。</param>
// <returns>來自worker方法的結果引數。</returns>
public static object Show(EventHandler<。 WaitWindowEventArgs> workerMethod, string message, params object[] args) {
List<object> arguments = new List<object> ()。
arguments.AddRange(args);
WaitWindow instance = new WaitWindow();
return instance.Show(workerMethod, message, arguments)。
}
#region Instance implementation
private WaitWindow(){}。
private WaitWindowGUI _GUI;
internal delegate void MethodInvoker< T>(T parameter1)/span>。
internal EventHandler<WaitWindowEventArgs> _WorkerMethod;
internal List<object> _Args;
// <summary>/span>
//更新等待視窗中顯示的資訊。
// </summary>/span>
public string Message{
set{
this._GUI.Invoke(new MethodInvoker<string> (this. _GUI.SetMessage),value)。)
}
// <summary>
// 取消作業并立即退出等待視窗。
// </summary>
public void Cancel(){
this._GUI.Invoke(new MethodInvoker(this._GUI.Cancel), null) 。
}
private object Show(EventHandler< WaitWindowEventArgs> workerMethod, string message, List<object> args){
//驗證引數。
if (workerMethod == null){
throw new ArgumentException("沒有指定作業者方法。", "workerMethod")。
} else {
this._WorkerMethod = workerMethod;
}
this._Args = args;
if (string.IsNullOrEmpty(message)){
message = "Please wait..."。
}
//設定視窗。
this._GUI = new WaitWindowGUI(this)。
this._GUI.MessageLabel.Text = message;
//呼叫它。
this._GUI.ShowDialog()。
object result = this._GUI._Result;
//清理
Exception _Error = this._GUI._Error;
this._GUI.Dispose()。
//回傳結果或拋出例外。
if (_Error != null){
throw _Error;
} else {
return result;
}
}
#endregion Instance implementation
}
}
如果有任何關于如何使其正常作業的建議,我們將不勝感激!!
uj5u.com熱心網友回復:
你不能使用服務器端的視窗。
但是,你可以做的是在用戶點擊按鈕的時候彈出或顯示一個對話框。當網頁在服務器上被處理時,該彈出對話框將保持打開。當服務器頁面處理完畢后,新的服務器頁面將被送回客戶端瀏覽器,因此顯示資訊將自行消失。
因此,假設你的啟動行程的按鈕是一個簡單的按鈕。當點擊這個 "較長 "的服務器端代碼時,就會運行。
因此,你保留了這個按鈕,但是添加了一個客戶端腳本來彈出一個對話資訊。 在這種情況下,我們將使用一個jQuery.UI對話框。 因此,你將需要在網頁中加入jQquery和jQuery.UI.
。所以,現在簡單的按鈕代碼變成了這樣:
<asp:Button ID="cmdBigProcess" runat="server" Text="start the reactor!"
OnClientClick="showait();return true;"
OnClick="cmdBigProcess_Click" />
<div id="mywait" style="display:none; border:solid">
<h2>大行程正在運行-請等待</h2>。
</div>
<腳本>
function showait() {
///讓我們彈出jquery.UI對話框。
var mydiv = $("#mywait"/span>)
mydiv.dialog({
modal: true, appendTo: "form",
標題。"Please wait", closeText: ""。
寬度:"400px"。
});
}
</script>
因此,我們添加了一個簡單的 "div",它將容納該資訊。
現在,當你點擊按鈕時,"on client click "代碼首先運行,并顯示對話框,然后運行你的服務器端代碼。如前所述,我們不必解散或對對話框做任何事情,因為當網頁被發布時,它將被傳送到服務器上。后面的代碼運行(可能會也可能不會改變網頁上的東西),然后當網頁完成后,整個網頁現在又回到了網頁端,瀏覽器重新規劃并重新加載網頁(這就是所謂的往返,或者通常稱為網頁生命周期。你掌握并理解這個往返的概念是非常關鍵的。你后面的代碼永遠不會與用戶直接互動。但是在點擊一個按鈕時,整個網頁就會被送到服務器上,代碼就會運行起來。甚至當代碼修改了網頁中的控制元件和東西時?它們不會顯示,不會更新,用戶也不會看到該網頁的任何變化,直到你后面的代碼全部完成。一旦后面的代碼完成,那么整個頁面就會回到瀏覽器中并被重新加載。
因此,頁面的重新加載實際上會導致彈出的對話框被駁回。請注意,jQuery.UI對話框(和大多數網路對話框)在被呼叫時不會停止代碼--代碼繼續。
結果是這樣的:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/334269.html
標籤:

