三種方法讓Response.Redirect在新窗口打開(kāi)
Response.Rederect在默認(rèn)情況下是在本頁(yè)跳轉(zhuǎn),所以除了在js中用window.open或是給A標(biāo)簽添加target屬性之外,在后臺(tái)似乎不能來(lái)打開(kāi)新的頁(yè)面,其實(shí)不然,通過(guò)設(shè)置form的target屬性同樣可以讓Response.Rederect所指向的url在新的窗口打開(kāi)。下面用三種方法來(lái)實(shí)現(xiàn)。
1 .給form指定target屬性,那么本頁(yè)面中所有的Response.Rederect都將在新的窗口中打開(kāi)。代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
form1.Target = "_blank";
}
或
<form id="form2" runat="server" target="_blank">
2 .用腳本針對(duì)某個(gè)控件來(lái)指定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#代碼:
[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");
}
}
}
上面的代碼中點(diǎn)擊button1在新窗口打開(kāi),點(diǎn)擊button2在本頁(yè)打開(kāi)。
3 .除了設(shè)置form的target屬性,要在新的窗口打開(kāi)頁(yè)面就只能用open,可以寫個(gè)通用的方法來(lái)實(shí)現(xiàn),如下:
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", "");第三個(gè)參數(shù)為open窗口的一些屬性。但這樣好像還不是很方便,在.net3.5中提供了擴(kuò)展方法的特性,在這里也可以借用一下,將上面的靜態(tài)方法實(shí)現(xiàn)為Response.Redirect的一個(gè)重載。具體代碼如下:
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);
}
}
}
將該類添加到項(xiàng)目中后,在程序中輸入Response.Redirect會(huì)發(fā)現(xiàn)該方法有三個(gè)重載了,這樣再結(jié)合前面的form的target 就非常方便了。
另外:
Respose.Write("<script language='javascript'>window.open('"+ url +"');</script>"); (打開(kāi)簡(jiǎn)潔窗口):
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)——直接轉(zhuǎn)向新的頁(yè)面,原窗口被代替;
2. Response.Write("<script>window.open('XXX.aspx','_blank')</script>")——原窗口保留,另外新增一個(gè)新頁(yè)面;
3. Response.Write("<script>window.location='XXX.aspx'</script>")——打開(kāi)新的頁(yè)面,原窗口被代替;
4. Server.Transfer("XXX.aspx")——打開(kāi)新的頁(yè)面;
5. Response.Write("<script>window.showModelessDialog('XXX.aspx')</script>")——原窗口保留,以對(duì)話框形式打開(kāi)新窗口;
6. Response.Write("<script>window.showModelDialog('XXX.aspx')</script>")——對(duì)話框形式打開(kāi)新窗口,原窗口被代替;
相關(guān)文章
ASP.NET 2.0/3.5中直接操作Gridview控件插入新記錄
Gridview控件中并沒(méi)有提供像在FormView和DetailsView控件中那樣直接插入新記錄操作的支持。2008-11-11asp.net中c#自定義事件的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了asp.net中c#自定義事件的實(shí)現(xiàn)方法,較為詳細(xì)的分析了自定義實(shí)現(xiàn)的各個(gè)步驟的具體實(shí)現(xiàn)思路與技巧,并給出了一個(gè)完整的實(shí)例總結(jié),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12asp.net下利用JS實(shí)現(xiàn)對(duì)后臺(tái)CS代碼的調(diào)用方法
asp.net下利用JS實(shí)現(xiàn)對(duì)后臺(tái)CS代碼的調(diào)用方法...2007-04-04淺談?wù)l都能看懂的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方式(附源碼)
這篇文章主要介紹了淺談?wù)l都能看懂的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方式(附源碼),具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12.net控件dropdownlist動(dòng)態(tài)綁定數(shù)據(jù)具體過(guò)程分解
一、在頁(yè)面初始化時(shí)候?qū)⒓辖壎ǖ紻ropDownList;二、在頁(yè)面初始化的時(shí)候向DropDownList添加數(shù)據(jù);三、將DataReader讀取的數(shù)據(jù)動(dòng)態(tài)綁定到DropDownList等等2013-05-05不使用web服務(wù)(Service)實(shí)現(xiàn)文本框自動(dòng)完成擴(kuò)展
以前寫Ajax 的AutoCompleteExtender功能,都需要寫WCF Service或是Web Service數(shù)據(jù)源,下面的演示,不用寫Service來(lái)實(shí)現(xiàn)文本框的AutoCompete extender功能,感興趣的朋友可以參考下哈2013-04-04.Net插件框架Managed Extensibility Framework簡(jiǎn)介
這篇文章介紹了.Net插件框架Managed Extensibility Framework,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07解決在Web.config或App.config中添加自定義配置的方法詳解
本篇文章是對(duì)在Web.config或App.config中添加自定義配置的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05.net 讀取項(xiàng)目AssemblyInfo.cs屬性值
.net 讀取項(xiàng)目AssemblyInfo.cs屬性值的實(shí)現(xiàn)代碼。2009-06-06基于.NET中建構(gòu)子中傳遞子對(duì)象的對(duì)象詳解
本篇文章介紹了,基于.NET中建構(gòu)子中傳遞子對(duì)象的對(duì)象詳解。需要的朋友參考下2013-05-05