[转]用Redirect从新窗口打开链接的方法
Redirect从新窗口打开页面小技巧:
果然有很多人需要这样做,本来我是想自己用Write一个Window.open来着,不过还是百度了一下,发现这位老兄封装的不错,就直接借用了.
看下面的这个方法
用的时候直接调用就可以了,哈哈
调用方法
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>");
果然有很多人需要这样做,本来我是想自己用Write一个Window.open来着,不过还是百度了一下,发现这位老兄封装的不错,就直接借用了.
看下面的这个方法
/// <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);
}
}
/// 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>");