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

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

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

<% @ Page Language = " C# "  AutoEventWireup = " true "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "
 
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< script runat = " server " >

    
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 " );
    }
</ script >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head id = " Head1 "  runat = " server " >
    
< title ></ title >
</ head >
< body id = " b "  runat = " server " >
< form id = " form1 "  runat = " server " >
    
< asp:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  Text = " 打开新窗口或者新 Tab  "   />
</ form >
</ body >
</ html >

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

<% @ Page Language = " C# "  AutoEventWireup = " true "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "
 
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< script runat = " server " >

    
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 " );
    }
</ script >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head id = " Head1 "  runat = " server " >
    
< title ></ title >
</ head >
< body id = " b "  runat = " server " >
< form id = " form1 "  runat = " server " >
    
< asp:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  Text = " 打开新窗口或者新 Tab  "   />
</ form >
</ body >
</ html >

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

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

<% @ Page Language = " C# "  AutoEventWireup = " true "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "
 
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< script runat = " server " >

    
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 " );
    }
</ script >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head id = " Head1 "  runat = " server " >
    
< title ></ title >
</ head >
< body id = " b "  runat = " server " >
< form id = " form1 "  runat = " server " >
    
< asp:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  Text = " 打开新窗口或者新 Tab  "   />
</ form >
</ body >
</ html >

另外一种弹出的方法可以参见老外的文章:

http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论
要实现打开窗口预览docx文件而不是下载,可以通过以下步骤进行操作。 首先,确保浏览器中已经安装了能够预览docx文件的插件或者默认的docx文件预览器。一般来说,现代浏览器都会自带默认的文档预览器。 然后,在使用window.open方法打开窗口时,需要将docx文件的URL作为参数传递给该方法。如下所示: ``` window.open('docx文件的URL'); ``` 这将导致浏览器尝试在窗口中预览docx文件。 但是,在某些情况下,浏览器可能会默认将docx文件以下载的方式打开,而不是在窗口中进行预览。这可能是因为浏览器的安全设置或者插件配置等原因。 为了确保能够以预览的方式打开docx文件,可以尝试设置响应头的Content-Disposition属性。具体而言,将其设置为"inline",表示请求的文件应在浏览器中内联显示,而不是作为附件下载。 如果是通过服务器返回docx文件的方式提供文件下载服务,可以在服务器端设置响应头,示例如下: ``` response.setHeader("Content-Disposition", "inline;filename=filename.docx"); ``` 其中,response为服务器返回给客户端的响应对象,"filename.docx"为要下载的docx文件的文件名。 总结起来,通过确保浏览器具有docx文件的预览插件或默认预览能力,并设置适当的Content-Disposition响应头,我们可以尝试使用window.open方法窗口中预览docx文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟子E章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值