本文主要是介绍Response.Redirect 打开新窗口的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方法一:在服务器端设置 target 属性,这个方法也非常适用于客户端不支持脚本的情况。代码如下:
protected void Page_Load(object sender, EventArgs e)
{
form1.Target = "_blank";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://dotnet.aspx.cc");
}
办法二:采用客户端脚本的方法设置 target 属性。代码如下:
复制 保存
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "this.form.target='_newName'");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://dotnet.aspx.cc");
}
方法三
protected void Page_Load(object sender, EventArgs e)
{
string WindowName = "win" + System.DateTime.Now.Ticks.ToString();
Page.RegisterOnSubmitStatement("js", "window.open('','" + WindowName + "','width=600,height=200')");
form1.Target = WindowName;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://dotnet.aspx.cc");
}
方法4:
public static class ResponseHelper
{
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 outside Page context.");
} url = page.ResolveClientUrl(url); string script; if (!String.IsNullOrEmpty(windowFeatures))
{ script = @"<script>window.open(""{0}"", ""{1}"", ""{2}"");</script>"; }
else
{
script = @"<script>window.open(""{0}"", ""{1}"");</script>";
}
script = String.Format(script, url, target, windowFeatures);
//ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
page.RegisterStartupScript("ddd", script);
}
}
}
调用:
ResponseHelper.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");
这篇关于Response.Redirect 打开新窗口的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!