轉自:http://www.cnblogs.com/oec2003/archive/2009/01/07/1371373.html
Response.Rederect在默認情況下是在本頁跳轉,所以除了在js中用window.open 或是給A標籤添加target屬性之外,在後臺似乎不能來打開新的頁面,其實不然,通過設置form的target屬性同樣可以讓Response.Rederect所指向的url在新的窗口打開。下面用三種方法來實現。
1 .給form指定target屬性,那麼本頁面中所有的Response.Rederect都將在新的窗口中打開。代碼如下:
protected void Page_Load(object sender, EventArgs e) { form1.Target = "_blank"; }
或
<form id="form2" runat="server" target="_blank">
2 .用腳本針對某個控件來指定form的target,代碼如下:
html代碼:
<%@ 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#代碼:
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"); } } }
上面的代碼中點擊button1在新窗口打開,點擊button2在本頁打開。
3 .除了設置form的target屬性,要在新的窗口打開頁面就只能用open,可以寫個通用的方法來實現,如下:
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); } } }
這樣就可以在程序中使用RedirectHelper.Redirect("oec2003.aspx", "_blank", ""); 第三個參數爲open窗口的一些屬性 。但這樣好像還不是很方便,在.net3.5中提供了擴展方法的特性,在這裏也可以借用一下,將上面的靜態方法實現爲Response.Redirect的一個重載。具體代碼如下:
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); } } }
將該類添加到項目中後,在程序中輸入Response.Redirect會發現該方法有三個重載了,這樣再結合前面的form的target 就非常方便了。