Response.Redirect 打开新窗口的两种方法
一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write("<script>window.location='http://dotnet.aspx.cc';</script>") 方法外,是不能在新窗口打开所指定的 URL 地址的。但是,如果仔细分析一下,如果设置 form 元素的 target 属性,还是有办法打开新窗口的。下面就是可以采用的两种方法。
方法一:在服务器端设置 target 属性,这个方法也非常适用于客户端不支持脚本的情况。代码如下:
<!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 属性。代码如下:
<!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 属性可以采用任何合法的名称,但要注意,如果相同名称的窗口已经打开,则新窗口会在已经存在名称的窗口里打开。
更新:如果需要设置弹出窗口的宽度和高度,可以修改为下面的方法:
<!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>
另外一种弹出的方法可以参见老外的文章:
Response.Redirect 打开新窗口的两种方法的更多相关文章
- Response.Redirect 打开新窗体的两种方法
普通情况下,Response.Redirect 方法是在server端进行转向,因此,除非使用 Response.Write("<script>window.location=' ...
- js打开新窗口的两种方式
1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a>等效于js代码wi ...
- Response.Write 打开新窗口
Response.Write 打开新窗口 string url=" XXX.aspx?aaa="+bbb"; Response.Write(" <scri ...
- 三种方法让Response.Redirect在新窗口打开
通过设置form的target属性同样可以让Response.Rederect所指向的url在新的窗口打开,下面为大家介绍三种具体的实现方法 Response.Rederect在默认情况下是在本页跳转 ...
- Response.Redirect在新窗口打开(转载)
Response.Rederect在默认情况下是在本页跳转,所以除了在js中用window.open或是给A标签添加target属性之外,在后台似乎不能来打开新的页面,其实不然,通过设置form的ta ...
- Response.Redirect在新窗口打开网页
来自:http://www.woosky.net/show.asp?id=761 Respose.Write("<script language='javascript'>win ...
- DataGridView动态添加新行的两种方法
简单介绍如何为DataGridView控件动态添加新行的两种方 法: 方法一: int index=this.dataGridView1.Rows.Add();this.dataGridView1.R ...
- windows 7中添加新硬件的两种方法(本地回环网卡)
最近在windows7上使用VMwareWorkstation7玩一些实验,遇到需要配置不同网络的问题. 因为在windows2003server上习惯使用要本地回环网卡了,那就想着在Windows7 ...
- Android驱动学习-APP操作新硬件的两种方法(支持添加的驱动)
在给Android添加新的驱动后,app要如何使用呢? 正常的使用一个设备,需要getService.但是像LED等我们自己添加的硬件驱动,Android源代码根本没有我们自己添加的服务. 第一种: ...
随机推荐
- pycharm ubuntu安装
https://www.cnblogs.com/iamjqy/p/7000874.html
- [hdu1712]ACboy needs your help分组背包
题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$ $for{\rm ...
- js链表操作
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 5、预测和鉴定miRNA的靶基因
转载:http://www.oebiotech.com/Article/mirnabjyyc.html http://www.ebiotrade.com/newsf/2014-9/2014925941 ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)
- hdu1056
#include <cstdio> void main(){ double length; double l[300]; l[1] = 1.0/2; int i; for (i = 2;; ...
- 【本人译作推荐】Windows 8应用开发:C#和XAML卷(原名:Building Windows 8 Apps with C# and XAML)
[图书推荐] 译名:Windows 8应用开发:C#和XAML卷 原名:Building Windows 8 Apps with C# and XAML 编辑推荐 国内第一本使用XAML与C#语言 ...
- Sharepoint商务智能学习笔记之Powerpivot Service Dmeo(八)
1)在Excel上添加Powerpivot工具栏 第一步,在Excel中启用Powerpivot 工具栏,新建一个空白Excel文件,在左上角点击文件,然后点击选项 2)使用Powerpivot添加数 ...
- LSI SAS 3008 Web配置操作
配置 LSI SAS 3008 介绍LSISAS3008的配置操作. 4.1 登录CU界面 介绍登录LSISAS3008的CU配置界面的方法. 4.2 创建RAID 介绍在LSISAS3008扣卡上创 ...
- Gson应用:从json格式简单字符串中获取value
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; i ...