欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-29 20:20:22
response.rederect在默认情况下是在本页跳转,所以除了在js中用window.open或是给a标签添加target属性之外,在后台似乎不能来打开新的页面,其实...

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>")——对话框形式打开新窗口,原窗口被代替;