如何在ASP.NET中的请求期间重定向到另一个页面

时间:2022-11-09 03:31:05

I have regular asp.net page with several buttons on it. I want to have 1 button something like Stop Request, so no matter what button were clicked user can click this button and get my page refreshed. So here's example of my idea:

我有常规的asp.net页面,上面有几个按钮。我希望有一个按钮,如停止请求,所以无论点击什么按钮,用户都可以点击此按钮,刷新我的页面。所以这是我的想法的例子:

<asp:Button ID="btnRefresh" runat="server" OnClick="btnRefresh_Click" ToolTip="Click to generate report based on parameters selected above"
                                        Text="Run Report" Width="100px" />
<asp:Button ID="btnCancelRequest" runat="server" Text="Stop Request" OnClientClick="javascript:return CancelRequestClick();"/>

So, btnRefresh makes a postback that executes SQL Queries and does back-end logic. Let's assume it takes 10-20 seconds for everything to complete. I have few more buttons like this. Some of them executes heavy sql queries and some of them makes some long time execution code on the back. I want to have btnCancelRequest button that will just refresh the page, which is will bring clean page back without any user changes.

因此,btnRefresh进行回发,执行SQL查询并执行后端逻辑。我们假设一切都需要10-20秒才能完成。我有更多像这样的按钮。其中一些执行繁重的SQL查询,其中一些查询会在后面生成一些长时间的执行代码。我想拥有btnCancelRequest按钮,它只会刷新页面,这将带来干净的页面,而无需任何用户更改。

Here's JS code

这是JS代码

function CancelRequestClick() {
     window.location.href("RateReport.aspx");
     return false;
};

Is there any way to make this JS code to be execute when it clicked and not to wait for response back?

有没有办法让这个JS代码在点击时执行而不是等待回复?

2 个解决方案

#1


0  

with javascript, window.stop(), stop the browser loading

使用javascript,window.stop(),停止浏览器加载

function CancelRequestClick() {
     window.stop();
     window.location.href("RateReport.aspx");
     return false;
};

without javascript, a little bit tricky, submiting a form can cancel the other request, for example a dummy submit to the same page

没有javascript,有点棘手,提交表单可以取消其他请求,例如虚拟提交到同一页面

<form action="YourAspxPage.aspx" method="get">
    <input type="submit" value="cancel">
</form>

pressing cancel will submit dummy form to that page, and stop the other process

按取消将向该页面提交虚拟表格,并停止其他进程

#2


0  

I want to have ability to cancel all current requests. If user click run button and it executes heave query on the back or user clicks button and it executes any other heave logic on the back I want to have ability to Cancel and refresh page at any time

我希望能够取消所有当前请求。如果用户单击运行按钮并在后面或用户单击按钮上执行升沉查询并且它在后面执行任何其他升沉逻辑我希望能够随时取消和刷新页面

First of all, HTTP is stateless. It means when a request is served, UI thread is not available anymore. As the result, you do not know that task to cancel.

首先,HTTP是无状态的。这意味着在提供请求时,UI线程不再可用。结果,您不知道要取消的任务。

What you are hoping to achieve is not possible. However, you have a work around. After triggering a long running task, you insert a record in database like -

您希望实现的目标是不可能的。但是,你有一个解决方法。触发长时间运行的任务后,在数据库中插入一条记录,如 -

UserId, TaskType, IsCancelled

Then let the task to go and check very often about the IsCancelled flag.

然后让任务去检查IsCancelled标志。

If you want to cancel it, set IsCancelled flag to true. When the task sees it, it will cancel itself until then you cannot do anything.

如果要取消它,请将IsCancelled标志设置为true。当任务看到它时,它会自动取消,直到那时你无法做任何事情。

Ideally, you want to use Scheduler like Hangfire or Quartz.Net for long running jobs, but it is out of the scope of your question.

理想情况下,您希望使用像Hangfire或Quartz.Net这样的Scheduler来执行长时间运行的作业,但这超出了您的问题的范围。

#1


0  

with javascript, window.stop(), stop the browser loading

使用javascript,window.stop(),停止浏览器加载

function CancelRequestClick() {
     window.stop();
     window.location.href("RateReport.aspx");
     return false;
};

without javascript, a little bit tricky, submiting a form can cancel the other request, for example a dummy submit to the same page

没有javascript,有点棘手,提交表单可以取消其他请求,例如虚拟提交到同一页面

<form action="YourAspxPage.aspx" method="get">
    <input type="submit" value="cancel">
</form>

pressing cancel will submit dummy form to that page, and stop the other process

按取消将向该页面提交虚拟表格,并停止其他进程

#2


0  

I want to have ability to cancel all current requests. If user click run button and it executes heave query on the back or user clicks button and it executes any other heave logic on the back I want to have ability to Cancel and refresh page at any time

我希望能够取消所有当前请求。如果用户单击运行按钮并在后面或用户单击按钮上执行升沉查询并且它在后面执行任何其他升沉逻辑我希望能够随时取消和刷新页面

First of all, HTTP is stateless. It means when a request is served, UI thread is not available anymore. As the result, you do not know that task to cancel.

首先,HTTP是无状态的。这意味着在提供请求时,UI线程不再可用。结果,您不知道要取消的任务。

What you are hoping to achieve is not possible. However, you have a work around. After triggering a long running task, you insert a record in database like -

您希望实现的目标是不可能的。但是,你有一个解决方法。触发长时间运行的任务后,在数据库中插入一条记录,如 -

UserId, TaskType, IsCancelled

Then let the task to go and check very often about the IsCancelled flag.

然后让任务去检查IsCancelled标志。

If you want to cancel it, set IsCancelled flag to true. When the task sees it, it will cancel itself until then you cannot do anything.

如果要取消它,请将IsCancelled标志设置为true。当任务看到它时,它会自动取消,直到那时你无法做任何事情。

Ideally, you want to use Scheduler like Hangfire or Quartz.Net for long running jobs, but it is out of the scope of your question.

理想情况下,您希望使用像Hangfire或Quartz.Net这样的Scheduler来执行长时间运行的作业,但这超出了您的问题的范围。