简体   繁体   English

Javascript无法使用Ajax ASP.Net进行response.redirect

[英]Javascript not working on response.redirect using ajax ASP.Net

I am redirecting the ASP.Net page to download page and i'm using ajax. 我将ASP.Net页面重定向到下载页面,并且我正在使用ajax。 The redirect page does work, the file is downloaded and the page that called the download is showing. 重定向页面确实起作用,下载了文件,并显示了称为下载的页面。 I need to call javascript after the file is downloaded to remove the waiting screen. 下载文件后,我需要调用javascript以删除等待屏幕。

I am first calling redirect then javascript to unblock the UI. 我先调用重定向,然后用JavaScript取消阻止用户界面。 to block the UI i'm doing that using onclientclick='block()'; 阻止UI我正在使用onclientclick ='block()';

  Response.Redirect("ExportFile.aspx, false);
 ScriptManager.RegisterClientScriptBlock((this), this.GetType(), "alertAction", "alert('');unblock();", true);

even the alert isn't working so the JS isn't being fired at all. 即使是警报也无法正常工作,因此JS根本不会被触发。 I tired putting the code on the file download page but that doesn't work either. 我厌倦了将代码放在文件下载页面上,但这也不起作用。 I even tried body onpageunload but that never fires either. 我什至尝试了body onpageunload,但也从未触发过。

How can I redirect to a file download page then call JS to unblock the UI? 如何重定向到文件下载页面,然后调用JS来取消阻止UI?

Once response.redirect happens, no other code can execute. 一旦response.redirect发生,其他代码将无法执行。 I would suggest to write your redirect code in javascript and put it into your RegisterClientScriptBlock. 我建议用javascript编写重定向代码,然后将其放入RegisterClientScriptBlock。 Just make sure the unblock is before the window.location. 只要确保取消阻塞在window.location之前即可。

ScriptManager.RegisterClientScriptBlock((this), this.GetType(), "alertAction", "unblock(); window.location.assign('yourPage.aspx');", true);

boruchsiper is correct. 硬汉是正确的。 Also, once you've redirected them, you're on a different page (if my assumption is correct that you're redirecting to a different page). 另外,一旦您将它们重定向到另一个页面(如果我的假设是正确的,那么您将重定向到另一个页面)。 You would want to fire off this line after arriving at the new page, so you'd want it in that page's code behind in the Load() function: 您可能希望在到达新页面后触发此行,因此您希望在Load()函数后面的该页面代码中使用它:

ScriptManager.RegisterClientScriptBlock((this), this.GetType(), "alertAction", "alert('');unblock();", true);

You may want to include a query string in that redirect URL to tell that page to take that action. 您可能希望在该重定向URL中包含一个查询字符串,以告知该页面执行该操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM