Three ways to let Response.Redirect open in a new window _ Practical Tips

Source: Internet
Author: User
Tags httpcontext reserved static class

Response.rederect is on this page by default, so in addition to using window.open in JS or adding target attributes to a tag, it doesn't seem to be able to open a new page in the background, but by setting the target property of the form you can also let RESPONSE.R The URL that Ederect points to opens in a new window. Here are three ways to implement this.

1. Assign the target attribute to the form, and all response.rederect in this page will open in a new window. The code is as follows:

Copy Code code 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 the target of the form for a control, the code is as follows:

HTML code:
Copy Code code as follows:

<%@ 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 ">

<title>ResponseRedirectDemo</title>
<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>

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, click Button2 on this page.

3. In addition to setting the target attribute of the form, open the page in a new window only with open, you can write a common method to implement, as follows:
Copy Code code 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 allows you to use Redirecthelper.redirect ("oec2003.aspx", "_blank", "") in your program, and the third parameter is some of the properties of the open window. But this seems not very convenient, in the. net3.5 provides the characteristics of the extension method, where you can also borrow, the above static method is implemented as a Response.Redirect overload. The specific code is as follows:
Copy Code code 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);
}
}
}

When you add this class to your project, entering Response.Redirect in your program will find that the method has three overloads, so it is convenient to combine target from the previous form.

Other than that:

Respose.write ("<script language= ' JavaScript ' >window.open (' + URL + ');</script>"); (Open simple 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 ("xxx.aspx", true)--directly to the new page, the original window is replaced;
2. Response.Write ("<script>window.open" (' xxx.aspx ', ' _blank ') </script> ")--the original window is reserved, another new page is added;
3. Response.Write ("<script>window.location= ' xxx.aspx ' </script>")--Opens the new page, replaces the original window;
4. Server.Transfer ("xxx.aspx")--open a new page;
5. Response.Write ("<script>window.showmodelessdialog (' xxx.aspx ') </script>")--the original window is reserved to open a new window in the form of a dialog box;
6. Response.Write ("<script>window.showmodeldialog (' xxx.aspx ') </script>")--dialog form opens a new window, the original window is replaced;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.