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 runat="server" target="_blank">
复制代码 代码如下:
<%@ 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 runat="server">
<title>ResponseRedirectDemo</title>
</head>
<body>
<form runat="server" target="_blank">
<div>
<asp:Button runat="server"
Text="OpenNewWindow"/>
<asp:Button runat="server"
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");
}
}
}
复制代码 代码如下:
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);
} } }
复制代码 代码如下:
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);
}
}
}
新闻热点
疑难解答
图片精选