您的位置:首页 > 其它

使用Response.Redirect打开新窗口的方法

2009-12-01 14:36 411 查看
  方法一:

  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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: