1. In general, Response. the Redirect method is switched on the server side, so unless Response. write ("<script> window. location = 'HTTP: // dotnet. aspx. cc '; </script> ") the specified URL address cannot be opened in a new window. However, if you carefully analyze it and set the target attribute of the form element, you can still open a new window. The following two methods can be used.
Method 1: set the target attribute on the server. This method is also very suitable when the client does not support scripts. The Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
Form1.Target = "_ blank ";
}
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("http://dotnet.aspx.cc ");
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body id = "B" runat = "server">
<Form id = "form1" runat = "server">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "open a new window or new Tab"/>
</Form>
</Body>
</Html>
Method 2: Use a client script to set the target attribute. The Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
Button1.Attributes. Add ("onclick", "this.form.tar get = '_ newname '");
}
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("http://dotnet.aspx.cc ");
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body id = "B" runat = "server">
<Form id = "form1" runat = "server">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "open a new window or new Tab"/>
</Form>
</Body>
</Html>
The target attribute in the above two methods can use any valid name, but note that if the window with the same name already opens, the new window will open in the window with the same name already exists.
Update: to set the width and height of the pop-up window, you can change it to the following method:
<% @ Page Language = "C #" AutoEventWireup = "true" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
String WindowName = "win" + System. DateTime. Now. Ticks. ToString ();
Page. RegisterOnSubmitStatement ("js", "window. open ('', '"+ WindowName +"', 'width = 600, height = 200 ')");
Form1.Target = WindowName;
}
Protected void button#click (object sender, EventArgs e)
{
Response. Redirect ("http://dotnet.aspx.cc ");
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body id = "B" runat = "server">
<Form id = "form1" runat = "server">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "open a new window or new Tab"/>
</Form>
</Body>
</Html>
Another pop-up method can be found in the article by foreigners:
Http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx
Here we will introduce how Ihttpmodule processes the request pipeline. The principle of Response. Redirect is that the server sets the returned http status code to 302, and then the client performs a jump. You can see the. net code segment using reflector.
The Redirect code in the Redirect method of the HttpResponse class is as follows:
This. StatusCode = 0x12e;
This. RedirectLocation = url;
Therefore, we can extend the Ihttpmodule to intercept the http Status Code, set it to 200, and insert a js Code segment to open a new window. The following is the implementation code of Ihttpmodule:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Namespace ClassLibrary2
{
Public class Class1: System. Web. IHttpModule
{
Public void Init (HttpApplication application)
{
Application. PreSendRequestHeaders + = new EventHandler (application_PreSendRequestHeaders );
}
Void application_PreSendRequestHeaders (object sender, EventArgs e)
{
HttpApplication Application = (HttpApplication) sender;
HttpContext context = Application. Context;
If (context. Response. StatusCode = 302)
{
Context. Response. Write ("<script> window. open ('" + context. Response. RedirectLocation + "') </script> ");
Context. Response. StatusCode = 200;
}
}
Public void Dispose ()
{
}
}
}
Finally, configure web. config.
<? Xml version = "1.0"?>
<Configuration>
<System. web>
<Compilation debug = "true"/>
<Authentication mode = "Windows"/>
<HttpModules>
<Add name = "MyModule" type = "ClassLibrary2.Class1, ClassLibrary2"> </add>
</HttpModules>
</System. web>
</Configuration>
Another point is that ms's self-sitting idea inserts such a piece of code before the end after the jump. I have to b4 them. Let's take a look at the solution.
This. Write ("This. write ("This. Write ("</body>
Very good network disk (http://ww618.com/-wangwang 618 wish you every day, every day. -_-)