我怎样才能让这个 c# 服务器端等待 window 在导出完成后退出?
how can i get this c# server side wait window to quit when exporting is finished?
团队!!
我有一个网站,人们可以将数据下载到 excel 或 pdf 文件中。下载时间足够长,以至于我想在处理过程中显示“请稍候”模式。我找到了一些执行此操作的代码,但问题是它只是从 1 计数到 100,然后退出。我想更改它,以便它在下载完成之前一直显示。
当用户单击按钮开始导出时,将调用 expot_click 函数。它下面的 WorkerMethod 函数被传递到 class 并且是从 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":
export_excel(exportSections, exportStudents);
break;
case "pdf":
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)
{
// Do something
for (int progress = 1; progress <= 100; progress++)
{
System.Threading.Thread.Sleep(20);
// Update the wait window message
e.Window.Message = string.Format
("Please wait ... {0}%", progress.ToString().PadLeft(3));
}
// Use the arguments sent in
if (e.Arguments.Count > 0)
{
// Set the result to return
e.Result = e.Arguments[0].ToString();
}
else
{
// Set the result to return
e.Result = "Hello World";
}
}
下面是被调用的class:
/*
* Created by SharpDevelop.
* User: mjackson
* Date: 05/03/2010
* Time: 09:36
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Jacksonsoft
{
/// <summary>
/// Displays a window telling the user to wait while a process is executing.
/// </summary>
public class WaitWindow
{
/// <summary>
/// Shows a wait window with the text 'Please wait...' while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <returns>The result argument from the worker method.</returns>
public static object Show(EventHandler<WaitWindowEventArgs> workerMethod){
return WaitWindow.Show(workerMethod, null);
}
/// <summary>
/// Shows a wait window with the specified text while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <param name="message">The text to display.</param>
/// <returns>The result argument from the worker method.</returns>
public static object Show(EventHandler<WaitWindowEventArgs> workerMethod, string message){
WaitWindow instance = new WaitWindow();
return instance.Show(workerMethod, message, new List<object>());
}
/// <summary>
/// Shows a wait window with the specified text while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <param name="message">The text to display.</param>
/// <param name="args">Arguments to pass to the worker method.</param>
/// <returns>The result argument from the worker method.</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);
internal EventHandler<WaitWindowEventArgs> _WorkerMethod;
internal List<object> _Args;
/// <summary>
/// Updates the message displayed in the wait window.
/// </summary>
public string Message{
set{
this._GUI.Invoke(new MethodInvoker<string>(this._GUI.SetMessage), value);
}
}
/// <summary>
/// Cancels the work and exits the wait windows immediately
/// </summary>
public void Cancel(){
this._GUI.Invoke(new MethodInvoker(this._GUI.Cancel), null);
}
private object Show(EventHandler<WaitWindowEventArgs> workerMethod, string message, List<object> args){
// Validate Parameters
if (workerMethod == null){
throw new ArgumentException("No worker method has been specified.", "workerMethod");
} else {
this._WorkerMethod = workerMethod;
}
this._Args = args;
if (string.IsNullOrEmpty(message)){
message = "Please wait...";
}
// Set up the window
this._GUI = new WaitWindowGUI(this);
this._GUI.MessageLabel.Text = message;
// Call it
this._GUI.ShowDialog();
object result = this._GUI._Result;
// clean up
Exception _Error = this._GUI._Error;
this._GUI.Dispose();
// Return result or throw and exception
if (_Error != null){
throw _Error;
} else {
return result;
}
}
#endregion Instance implementation
}
}
如有任何建议,我们将不胜感激!!
您不能使用服务器端 window。
但是,您可以做的是在用户单击按钮时弹出或显示一个对话框。当网页在正在处理的服务器上打开时,该弹出对话框将保持打开状态。服务器页面完成后,新的新服务器页面将发送回客户端浏览器,因此显示消息将自行消失。
因此,假设您的启动流程按钮是一个简单的按钮。单击“更长”的服务器端代码时运行。
因此,您保留按钮,但添加客户端脚本以弹出对话框消息。
在这种情况下,我们将使用 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>Big process running - please wait</h2>
</div>
<script>
function showait() {
// lets pop jquery.UI dialog
var mydiv = $("#mywait")
mydiv.dialog({
modal: true, appendTo: "form",
title: "Please wait", closeText: "",
width: "400px"
});
}
</script>
因此,我们添加了一个简单的“div”来保存消息。
现在,当您单击按钮时,“在客户端单击”代码首先运行并显示对话框,然后您的服务器端代码运行。如前所述,我们不必关闭对话框或对对话框执行任何操作,因为当网页发布时,它会向上传送到服务器。后面的代码运行(可能会或可能不会改变网页上的内容),然后当页面完成时,整个网页现在返回到网页端,浏览器重新绘制并重新加载网页(这就是所谓的往返,或者通常称为页面生命周期。掌握和理解这个往返概念是非常重要的。你背后的代码从不直接与用户交互。但是一旦按下按钮,整个网络页面传输到服务器,代码运行。即使代码修改了网页中的控件和内容?它们不会显示,不会更新,并且用户不会看到该网页的任何更改,直到您所有的代码都被删除完成。一旦代码隐藏完成,整个页面就会返回浏览器并重新加载。
因此,页面的重新加载实际上将关闭弹出的对话框。请注意,jQuery.UI 对话框(和大多数 Web 对话框)在调用时不会停止代码 - 代码会继续。
结果如下所示:
团队!!
我有一个网站,人们可以将数据下载到 excel 或 pdf 文件中。下载时间足够长,以至于我想在处理过程中显示“请稍候”模式。我找到了一些执行此操作的代码,但问题是它只是从 1 计数到 100,然后退出。我想更改它,以便它在下载完成之前一直显示。
当用户单击按钮开始导出时,将调用 expot_click 函数。它下面的 WorkerMethod 函数被传递到 class 并且是从 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":
export_excel(exportSections, exportStudents);
break;
case "pdf":
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)
{
// Do something
for (int progress = 1; progress <= 100; progress++)
{
System.Threading.Thread.Sleep(20);
// Update the wait window message
e.Window.Message = string.Format
("Please wait ... {0}%", progress.ToString().PadLeft(3));
}
// Use the arguments sent in
if (e.Arguments.Count > 0)
{
// Set the result to return
e.Result = e.Arguments[0].ToString();
}
else
{
// Set the result to return
e.Result = "Hello World";
}
}
下面是被调用的class:
/*
* Created by SharpDevelop.
* User: mjackson
* Date: 05/03/2010
* Time: 09:36
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Jacksonsoft
{
/// <summary>
/// Displays a window telling the user to wait while a process is executing.
/// </summary>
public class WaitWindow
{
/// <summary>
/// Shows a wait window with the text 'Please wait...' while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <returns>The result argument from the worker method.</returns>
public static object Show(EventHandler<WaitWindowEventArgs> workerMethod){
return WaitWindow.Show(workerMethod, null);
}
/// <summary>
/// Shows a wait window with the specified text while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <param name="message">The text to display.</param>
/// <returns>The result argument from the worker method.</returns>
public static object Show(EventHandler<WaitWindowEventArgs> workerMethod, string message){
WaitWindow instance = new WaitWindow();
return instance.Show(workerMethod, message, new List<object>());
}
/// <summary>
/// Shows a wait window with the specified text while executing the passed method.
/// </summary>
/// <param name="workerMethod">Pointer to the method to execute while displaying the wait window.</param>
/// <param name="message">The text to display.</param>
/// <param name="args">Arguments to pass to the worker method.</param>
/// <returns>The result argument from the worker method.</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);
internal EventHandler<WaitWindowEventArgs> _WorkerMethod;
internal List<object> _Args;
/// <summary>
/// Updates the message displayed in the wait window.
/// </summary>
public string Message{
set{
this._GUI.Invoke(new MethodInvoker<string>(this._GUI.SetMessage), value);
}
}
/// <summary>
/// Cancels the work and exits the wait windows immediately
/// </summary>
public void Cancel(){
this._GUI.Invoke(new MethodInvoker(this._GUI.Cancel), null);
}
private object Show(EventHandler<WaitWindowEventArgs> workerMethod, string message, List<object> args){
// Validate Parameters
if (workerMethod == null){
throw new ArgumentException("No worker method has been specified.", "workerMethod");
} else {
this._WorkerMethod = workerMethod;
}
this._Args = args;
if (string.IsNullOrEmpty(message)){
message = "Please wait...";
}
// Set up the window
this._GUI = new WaitWindowGUI(this);
this._GUI.MessageLabel.Text = message;
// Call it
this._GUI.ShowDialog();
object result = this._GUI._Result;
// clean up
Exception _Error = this._GUI._Error;
this._GUI.Dispose();
// Return result or throw and exception
if (_Error != null){
throw _Error;
} else {
return result;
}
}
#endregion Instance implementation
}
}
如有任何建议,我们将不胜感激!!
您不能使用服务器端 window。
但是,您可以做的是在用户单击按钮时弹出或显示一个对话框。当网页在正在处理的服务器上打开时,该弹出对话框将保持打开状态。服务器页面完成后,新的新服务器页面将发送回客户端浏览器,因此显示消息将自行消失。
因此,假设您的启动流程按钮是一个简单的按钮。单击“更长”的服务器端代码时运行。
因此,您保留按钮,但添加客户端脚本以弹出对话框消息。 在这种情况下,我们将使用 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>Big process running - please wait</h2>
</div>
<script>
function showait() {
// lets pop jquery.UI dialog
var mydiv = $("#mywait")
mydiv.dialog({
modal: true, appendTo: "form",
title: "Please wait", closeText: "",
width: "400px"
});
}
</script>
因此,我们添加了一个简单的“div”来保存消息。
现在,当您单击按钮时,“在客户端单击”代码首先运行并显示对话框,然后您的服务器端代码运行。如前所述,我们不必关闭对话框或对对话框执行任何操作,因为当网页发布时,它会向上传送到服务器。后面的代码运行(可能会或可能不会改变网页上的内容),然后当页面完成时,整个网页现在返回到网页端,浏览器重新绘制并重新加载网页(这就是所谓的往返,或者通常称为页面生命周期。掌握和理解这个往返概念是非常重要的。你背后的代码从不直接与用户交互。但是一旦按下按钮,整个网络页面传输到服务器,代码运行。即使代码修改了网页中的控件和内容?它们不会显示,不会更新,并且用户不会看到该网页的任何更改,直到您所有的代码都被删除完成。一旦代码隐藏完成,整个页面就会返回浏览器并重新加载。
因此,页面的重新加载实际上将关闭弹出的对话框。请注意,jQuery.UI 对话框(和大多数 Web 对话框)在调用时不会停止代码 - 代码会继续。
结果如下所示: