Three ways to make Response.Redirect open in a new window

  • 2020-07-21 07:26:01
  • OfStack

By default, Response.Rederect jumps to this page, so it doesn't seem to be possible to open a new page in the background, except for using window.open in js or adding the target attribute to the A tag. In fact, setting the target attribute of form can also make the url pointed by ES10en.Rederect open in a new window. Here are three ways to do it.

1. Assign the target attribute to form so that all Response.Rederect on this page will open in a new window. The code is as follows:

 
protected void Page_Load(object sender, EventArgs e) 
{ 
form1.Target = "_blank"; 
} 
 or  
<form id="form2" runat="server" target="_blank"> 

2. Use a script to specify target for form for a control, as follows:

html code:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ResponseRedirectDemo._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
<title>ResponseRedirectDemo</title> 
</head> 
<body> 
<form id="form2" runat="server" target="_blank"> 
<div> 
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
Text="OpenNewWindow"/> 
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" 
Text="OpenOldWindow" /> 
</div> 
</form> 
</body> 
</html> 

C# Code:  
[code] 
namespace ResponseRedirectDemo 
{ 
public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
Button1.Attributes.Add("onclick", "this.form.target='_blank'"); 
Button2.Attributes.Add("onclick", "this.form.target=''"); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
Response.Redirect("http://oec2003.cnblogs.com"); 
} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
Response.Redirect("http://oec2003.cnblogs.com"); 
} 
} 
} 

Click button1 in the above code to open in a new window, and click button2 to open in this page.

3. In addition to setting the target property of form, you can only use open to open the page in a new window. You can write a general method to achieve this, as follows:
 
public class RedirectHelper 
{ 
public static void Redirect(string url, 
string target, string windowFeatures) 
{ 
HttpContext context = HttpContext.Current; 
if ((String.IsNullOrEmpty(target) || 
target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && 
String.IsNullOrEmpty(windowFeatures)) 
{ 
context.Response.Redirect(url); 
} 
else 
{ 
Page page = (Page)context.Handler; 
if (page == null) 
{ 
throw new 
InvalidOperationException("Cannot redirect to new window."); 
} 
url = page.ResolveClientUrl(url); 
string script; 
if (!String.IsNullOrEmpty(windowFeatures)) 
{ 
script = @"window.open(""{0}"", ""{1}"", ""{2}"");"; 
} 
else 
{ 
script = @"window.open(""{0}"", ""{1}"");"; 
} 
script = String.Format(script, url, target, windowFeatures); 
page.ClientScript.RegisterStartupScript(page.GetType(), 
"Redirect", script, true); 
} } } 

This makes it possible to use ES40en.Redirect (" es42EN2003.aspx ", "_blank", "") in the program; The third parameter is some properties of the open window. However, this is not very convenient, es46EN 3.5 provides the extension method features, here can also borrow 1, the above static method as Response. Redirect overload. The specific code is as follows:
 
public static class RedirectHelper 
{ 
public static void Redirect(this HttpResponse response, 
string url, string target, string windowFeatures) 
{ 
if ((String.IsNullOrEmpty(target) || 
target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && 
String.IsNullOrEmpty(windowFeatures)) 
{ 
response.Redirect(url); 
} 
else 
{ 
Page page = (Page)HttpContext.Current.Handler; if (page == null) 
{ 
throw new 
InvalidOperationException("Cannot redirect to new window ."); 
} 
url = page.ResolveClientUrl(url); 
string script; 
if (!String.IsNullOrEmpty(windowFeatures)) 
{ 
script = @"window.open(""{0}"", ""{1}"", ""{2}"");"; 
} 
else 
{ 
script = @"window.open(""{0}"", ""{1}"");"; 
} 
script = String.Format(script, url, target, windowFeatures); 
ScriptManager.RegisterStartupScript(page, 
typeof(Page), "Redirect", script, true); 
} 
} 
} 

Once you've added this class to your project, enter Response.Redirect into your program and you'll see that the method has three overloads, which makes it convenient to combine target from form before.

In addition:

Respose.Write(" < script language='javascript' > window.open('"+ url +"'); < /script > "); (Open the concise window) :
Respose.Write(" < script language='javascript' > window.open('" + url + "','','resizable=1,scrollbars=0,status=1,menubar=no,toolbar=no,location=no, menu=no'); < /script > ");

1. Response. Redirect(" ES82en. aspx",true) -- go directly to the new page and the original window is replaced;
2. Response.Write(" < script > window.open('XXX.aspx','_blank') < /script > ") -- Keep the original window and add a new page;
3. Response.Write(" < script > window.location='XXX.aspx' < /script > ") -- Open a new page, the original window will be replaced;
4. Server. Transfer(" ES106en. aspx") -- open a new page;
5. Response.Write(" < script > window.showModelessDialog('XXX.aspx') < /script > ") -- Keep the original window and open the new window in the form of dialog box;
6. Response.Write(" < script > window.showModelDialog('XXX.aspx') < /script > ") -- Open a new window in the form of dialog box, and the original window will be replaced;


Related articles: