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

  • 相关阅读:
    JAVA动态代理
    图解 Tomcat 体系结构
    AdvancedDataGrid的使用
    You have an error in your SQL syntax; check the manual that corresponds...错误解决方案
    更新整站索引时失败,错误原因: [Incorrect integer value: `` for column `uptime` at row 1]
    dedecms转换v9 卡住的解决办法
    IIS 涉及到500和403或者404友好错误的设置!
    PHP Warning: date(): It is not safe to rely on the system's timezone settings
    不能读取记录;在 'MSysObjects' 上没有读取数据权限
    PHP5.3.5如何连接MSSql Server2005
  • 原文地址:https://www.cnblogs.com/qiu18359243869/p/12724148.html
Copyright © 2011-2022 走看看