Response. by default, Rederect redirects to this page. Therefore, besides using Windows in js. in addition to the open or add target attribute to tag A, it seems that you cannot open A new page in the background. Otherwise, you can set the target attribute of form to make Response. the url to which Rederect points is opened in a new window. The following three methods are used.
1. Specify the target attribute for form. All Response. Rederect on this page will be opened in a new window. The Code is as follows:
Copy codeThe 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 the target of a form for a control. The Code is as follows:
Html code:
Copy codeThe Code is 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">
<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 = "button#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.tar get = '_ blank '");
Button2.Attributes. Add ("onclick", "this.form.tar get = ''");
}
Protected void button#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 ");
}
}
}
In the code above, click button1 to open in the new window, and click button2 to open on this page.
3. In addition to setting the target attribute of form, to open the page in a new window, you can only use open. You can write a common method to implement it, as shown below:
Copy codeThe Code is 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 );
}}}
In this way, you can use RedirectHelper. Redirect ("oec2003.aspx", "_ blank", "") in the program. The third parameter is some attributes of the open window. However, it seems that this is not very convenient. In. net3.5, the feature of the extension method is provided. Here you can also use it to implement the above static method as an overload of Response. Redirect. The Code is as follows:
Copy codeThe 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 );
}
}
}
After the class is added to the project, input Response. Redirect in the program and you will find that there are three overload methods in this method. In this way, it is very convenient to combine them with the target of the previous form.
In addition:
Respose. Write ("<script language = 'javascript '> window. open ('" + url + "'); </script>"); (open a 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 ("XXX. aspx", true) -- go directly to the new page and replace the original window;
2. response. write ("<script> window. open ('xxx. aspx ',' _ blank ') </script> ") -- the original window is retained, and a new page is added;
3. Response. Write ("<script> window. location = 'xxx. aspx '</script>") -- open a new page and replace the original window;
4. Server. Transfer ("XXX. aspx") -- open a new page;
5. Response. Write ("<script> window. showModelessDialog ('xxx. aspx ') </script>") -- retain the original window and open a new window in the form of a dialog box;
6. Response. Write ("<script> window. showModelDialog ('xxx. aspx ') </script>") -- opens a new window in the dialog box, and the original window is replaced;