1、一般情況下,Response.Redirect 方法是在伺服器端進行轉向,因此,除非使用 Response.Write("<script>window.location='http://dotnet.aspx.cc';</script>") 方法外,是不能在新視窗開啟所指定的 URL 地址的。但是,如果仔細分析一下,如果設定 form 元素的 target 屬性,還是有辦法開啟新視窗的。下面就是可以採用的兩種方法。
方法一:在伺服器端設定 target 屬性,這個方法也非常適用於用戶端不支援指令碼的情況。代碼如下:
<%@ 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 Button1_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="Button1_Click" Text="開啟新視窗或者新 Tab " />
</form>
</body>
</html>
辦法二:採用用戶端指令碼的方法設定 target 屬性。代碼如下:
<%@ 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.target='_newName'");
}
protected void Button1_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="Button1_Click" Text="開啟新視窗或者新 Tab " />
</form>
</body>
</html>
上面兩種方法中的 target 屬性可以採用任何合法的名稱,但要注意,如果相同名稱的視窗已經開啟,則新視窗會在已經存在名稱的視窗裡開啟。
更新:如果需要設定快顯視窗的寬度和高度,可以修改為下面的方法:
<%@ 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 Button1_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="Button1_Click" Text="開啟新視窗或者新 Tab " />
</form>
</body>
</html>
另外一種彈出的方法可以參見老外的文章:
http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx
這裡介紹Ihttpmodule處理請求管道的方法。Response.Redirect的原理是伺服器設定返回http狀態代碼為302,然後用戶端執行跳轉,用reflector可以看到.net的程式碼片段。
以下是HttpResponse類的Redirect方法中的跳轉代碼
this.StatusCode = 0x12e;
this.RedirectLocation = url;
所以,我們可以擴充Ihttpmodule來攔截http狀態代碼,強制設為200,同時插入js程式碼片段來實現新視窗開啟。以下是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()
{
}
}
}
最後配置一下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>
另外一點,ms自坐主張的在跳轉之後,end之前插入了這麼一段代碼,我得b4他們一下,解決辦法,大家自己想想吧。
this.Write("<html><head><title>Object moved</title></head><body> ");
this.Write("<h2>Object moved to <a href="" + HttpUtility.HtmlAttributeEncode(url) + "">here</a>.</h2> ");
this.Write("</body></html> ");
很不錯的網盤(http://ww618.com/-WangWang618 祝博友們每天旺,每天發。-_-)