简体   繁体   English

Response.Redirect之后的Javascript alert()

[英]Javascript alert() after Response.Redirect

I am calling this in my code behind: 我在后面的代码中调用它:

(test.aspx)
Response.Redirect("~/Default.aspx");

I want to include a javascript alert after/before being redirected to Default.aspx, is it possible? 我想在重定向到Default.aspx之后/之前包含一个javascript警报,是否可能? I'm doing this because I'm passing a value to another page (test.aspx) and that page checks the db, if reader HasRow(), then redirect to Default.aspx. 我这样做是因为我将一个值传递给另一个页面(test.aspx)并且该页面检查数据库,如果是读者HasRow(),则重定向到Default.aspx。

这样做的方法是使用javascript显示警报,然后使用javascript进行重定向:

ScriptManager.RegisterStartupScript(this,this.GetType(),"Redit","alert('asdf'); window.location='" + Request.ApplicationPath + "Default.aspx';",true);

Lets take a look at what happens when you call response.redirect() 让我们来看看调用response.redirect()时会发生什么

  1. Servers sends HTTP response 302 to browser including the url to navigate to 服务器将HTTP响应302发送到浏览器,包括要导航到的URL
  2. browser doesn't display content of response and instead gets the content from the url specified 浏览器不显示响应内容,而是从指定的URL获取内容
  3. if the request to the new url succeeds, then it is displayed. 如果对新URL的请求成功,则显示。

Now looking at this, we can deduce that it is impossible to tell the browser to do a alert() from the page that issues the redirect because its content (if any) is discarded. 现在看一下,我们可以推断,不可能告诉浏览器从发出重定向页面做一个alert(),因为它的内容(如果有的话)被丢弃了。

It is possible to accomplish what you want from the page that you are redirecting to . 可以从要重定向到的页面中完成所需的操作 To do this, just check Request.UrlReferrer to check if you were redirected from the correct page, then display the alert when appropriate. 要执行此操作,只需检查Request.UrlReferrer以检查是否从正确的页面重定向,然后在适当时显示警报。

example: 例:

  1. Page1 redirects to page2 Page1重定向到第2页
  2. Page2 check if Request.UrlReferrer is equal to page1 第2页检查Request.UrlReferrer是否等于page1
  3. If equal, display alert 如果相等,则显示警报
  4. If not, do nothing special 如果没有,不做任何特别的事

Another approach is do the alert first then do the redirect from javascript. 另一种方法是首先进行警报,然后从javascript进行重定向。 window.location.href = newurl. window.location.href = newurl。

if you have display something message on Defaul.aspx page you must declare it. 如果您在Defaul.aspx页面上显示了某些消息,则必须声明它。 Because when you use redirect your page is rendering from top. 因为当您使用重定向时,您的页面将从顶部呈现。 You must set before redirect Sesion state on something flag and on Default.aspx page you must insert section who been added when this Sesion state been set. 您必须在重定向Sesion状态之前设置某个标志,并且在Default.aspx页面上,您必须插入在设置此Sesion状态时添加的部分。

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

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