三种方法让Response.Redirect在新窗口打开

所属分类: 网络编程 / ASP.NET 阅读数: 1071
收藏 0 赞 0 分享

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#代码:
[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");
}
}
}

上面的代码中点击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 就非常方便了。

另外:

Respose.Write("<script language='javascript'>window.open('"+ url +"');</script>"); (打开简洁窗口):
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)——直接转向新的页面,原窗口被代替;
2. Response.Write("<script>window.open('XXX.aspx','_blank')</script>")——原窗口保留,另外新增一个新页面;
3. Response.Write("<script>window.location='XXX.aspx'</script>")——打开新的页面,原窗口被代替;
4. Server.Transfer("XXX.aspx")——打开新的页面;
5. Response.Write("<script>window.showModelessDialog('XXX.aspx')</script>")——原窗口保留,以对话框形式打开新窗口;
6. Response.Write("<script>window.showModelDialog('XXX.aspx')</script>")——对话框形式打开新窗口,原窗口被代替;

更多精彩内容其他人还在看

MVC5下拉框绑定的方法(单选)

这篇文章主要为大家详细介绍了MVC5下拉框绑定,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

.NET Core 2.0 Preview2 发布汇总

这篇文章主要为大家详细介绍了.NET Core 2.0 Preview2 发布汇总的相关内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

MVVM模式下WPF动态绑定展示图片

这篇文章主要为大家详细介绍了MVVM模式下WPF动态绑定展示图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Visual Studio Debugger七个鲜为人知的小功能

这篇文章主要为大家详细介绍了Visual Studio Debugger七个鲜为人知的小功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

asp.net core中灵活的配置方式详解

这篇文章主要给的阿加介绍了关于在asp.net core中灵活的配置方式的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
收藏 0 赞 0 分享

详解使用asp.net mvc部分视图渲染html

为了提升用户体验,一般我们采用ajax加载数据然后根据数据渲染html,渲染html可以使用前端渲染和服务器端渲染,有兴趣的可以了解一下
收藏 0 赞 0 分享

WPF实现定时刷新UI界面功能

这篇文章主要为大家详细介绍了WPF实现定时刷新UI界面功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

.net接入支付宝的支付接口

这篇文章主要为大家详细介绍了.net接入支付宝的支付接口,H5网站接入支付宝的支付接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Visual Studio 2017 针对移动开发的新特性汇总

Visual Studio是世界上最好的IDE之一,下面就让我们一起来看看Visual Studio 2017中有哪些功能使得移动开发变得更加容易,感兴趣的朋友通过本文学习下吧
收藏 0 赞 0 分享

ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

本文主要介绍了ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据传递的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多