zoukankan      html  css  js  c++  java
  • respones.redirect 打开新的页面的两个方法

    转载Response.Redirect 打开新窗口的两种方法

    一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write("<script>window.location='http://dotnet.aspx.cc';</script>") 方法外,是不能在新窗口打开所指定的  URL 地址的。但是,如果仔细分析一下,如果设置 form 元素的 target 属性,还是有办法打开新窗口的。下面就是可以采用的两种方法。

    方法一:在服务器端设置 target 属性,这个方法也非常适用于客户端不支持脚本的情况。代码如下:

    1.  
      <%@ Page Language="C#" AutoEventWireup="true" %> 
    2.  
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    3.  
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    4.  
      <script runat="server"> 
    5.  
      protected void Page_Load(object sender, EventArgs e) 
    6.  
          { 
    7.  
              form1.Target = "_blank"; 
    8.  
          } 
    9.  
      protected void Button1_Click(object sender, EventArgs e) 
    10.  
          { 
    11.  
              Response.Redirect("http://dotnet.aspx.cc"); 
    12.  
          } 
    13.  
      </script> 
    14.  
      <html xmlns="http://www.w3.org/1999/xhtml"> 
    15.  
      <head id="Head1" runat="server"> 
    16.  
      <title></title> 
    17.  
      </head> 
    18.  
      <body id="b" runat="server"> 
    19.  
      <form id="form1" runat="server"> 
    20.  
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打开新窗口或者新 Tab " /> 
    21.  
      </form> 
    22.  
      </body> 
    23.  
      </html>

    办法二:采用客户端脚本的方法设置 target 属性。代码如下:

    1.  
      <%@ Page Language="C#" AutoEventWireup="true" %> 
    2.  
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    3.  
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    4.  
      <script runat="server"> 
    5.  
      protected void Page_Load(object sender, EventArgs e) 
    6.  
          { 
    7.  
              Button1.Attributes.Add("onclick", "this.form.target='_newName'"); 
    8.  
          } 
    9.  
      protected void Button1_Click(object sender, EventArgs e) 
    10.  
          { 
    11.  
              Response.Redirect("http://dotnet.aspx.cc"); 
    12.  
          } 
    13.  
      </script> 
    14.  
      <html xmlns="http://www.w3.org/1999/xhtml"> 
    15.  
      <head id="Head1" runat="server"> 
    16.  
      <title></title> 
    17.  
      </head> 
    18.  
      <body id="b" runat="server"> 
    19.  
      <form id="form1" runat="server"> 
    20.  
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打开新窗口或者新 Tab " /> 
    21.  
      </form> 
    22.  
      </body> 
    23.  
      </html>

    上面两种方法中的 target 属性可以采用任何合法的名称,但要注意,如果相同名称的窗口已经打开,则新窗口会在已经存在名称的窗口里打开。

    更新:如果需要设置弹出窗口的宽度和高度,可以修改为下面的方法:

    1.  
      <%@ Page Language="C#" AutoEventWireup="true" %> 
    2.  
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    3.  
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    4.  
      <script runat="server"> 
    5.  
      protected void Page_Load(object sender, EventArgs e) 
    6.  
          { 
    7.  
      string WindowName = "win" + System.DateTime.Now.Ticks.ToString(); 
    8.  
            Page.RegisterOnSubmitStatement("js", "window.open('','" + WindowName + "','width=600,height=200')"); 
    9.  
              form1.Target = WindowName; 
    10.  
          } 
    11.  
      protected void Button1_Click(object sender, EventArgs e) 
    12.  
          { 
    13.  
              Response.Redirect("http://dotnet.aspx.cc"); 
    14.  
          } 
    15.  
      </script> 
    16.  
      <html xmlns="http://www.w3.org/1999/xhtml"> 
    17.  
      <head id="Head1" runat="server"> 
    18.  
      <title></title> 
    19.  
      </head> 
    20.  
      <body id="b" runat="server"> 
    21.  
      <form id="form1" runat="server"> 
    22.  
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打开新窗口或者新 Tab " /> 
    23.  
      </form> 
    24.  
      </body> 
    25.  
      </html>

    另外一种弹出的方法可以参见老外的文章:
    http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx

     

    转载于:https://my.oschina.net/SnifferApache/blog/122430

  • 相关阅读:
    听说你的MES系统又失败了?
    GROUP BY你都不会!ROLLUP,CUBE,GROUPPING详解
    对 MES 感兴趣?赶紧看过来!
    SQL 高级查询(层次化查询,递归)
    智能制造概念
    简单又实用的分享!SharePoint母版页引用(实战)
    原创分享!SharePoint母版页修改(实战)
    入门者必看!SharePoint之CAML总结(实战)
    新手必看!Office Web Apps 2013 安装与配置(实战)
    SharePoint布局页引用(实战)
  • 原文地址:https://www.cnblogs.com/qiu18359243869/p/12724148.html
Copyright © 2011-2022 走看看