Code
/// <summary>
/// Redirect从新窗口打开
/// </summary>
/// <param name="url">重定向的URL地址</param>
/// <param name="target">target目标</param>
/// <param name="windowFeatures">窗体属性设置</param>
public static void Redirect(string url, string target, string windowFeatures)
{
if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase))&&String.IsNullOrEmpty(windowFeatures))
{
HttpContext.Current.Response.Redirect(url);
}
else
{
Page page = (Page)HttpContext.Current.Handler;
if (page == null)
{
throw new InvalidOperationException(
"不能重定向到新的窗口");
}
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);
ClientScriptManager csM = page.ClientScript;
csM.RegisterStartupScript(typeof(Page), "Redirect", script, true);
}
}
用的时候直接调用就可以了
调用方法
windowFeatures是设置窗体,可以为空
Redirect("http://www.baidu.com/", "_blank","");
Redirect("http://www.baidu.com/", "_blank", "'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no'");
其实也是利用了window.open() , 换而言之,也可以用
Response.Writer("<script language=javascript>window.open('test.html','newwindow','');</script>");