.NET 中如何弹出新页面,而不出现阻止
2011-02-22 17:33
316 查看
一.用Response.Redirect()
比如在这个页面中有一个DropDownList 和 一个button ,dropdownlist 的value为一些页面的URL,当选好后,点击Button后,转向dropdownlist 的value对应的页面。
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Button1.Attributes.Add("onclick","this.form.target = '_blank'");
}
protected void Button1_Click(object sender, EventArgs e)
{
string url = "http://www.baidu.com";
this.Response.Redirect(url);
}
}
二. 用Ajax实现
1、 在web编程过程中,经常会遇到一些页面需要弹出窗口,但是在服务器端用window.open弹出的窗口会被IE阻止掉,showModalDialog弹出的窗口有时并不能满足我们需要,我们需要弹出新的浏览器窗口。
2、 为什么我们编写的弹出窗口会被IE阻止呢,原来IE会自动判断弹出窗口的状态,它会阻止自动弹出的窗口,而通过我们用鼠标点击弹出的窗口,它是不会阻止的。这里就有一个问题,有人说:我的程序是写在服务器按钮里的,也是通过鼠标点击弹出的呀!其实只有在加载页面后,我们点击到弹出这段时间页面没有被重新加载的情况下,弹出的窗口才不会被阻止!这也就是说,写在服务器控件的回传事件里的window.open都会被阻止。
3、 问题搞清楚了事情也就好办了,我们可以用ajax来与服务器端通信,在客户端用javascript来编写事件处理程序,这样在客户端的就不会因为回传事件而重新加载页面,这样弹出窗口就不会被阻止了!
如:
Default.aspx:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<script language ="javascript" >
function getAjaxXml()
{
var xml = new ActiveXObject("Microsoft.XMLHTTP");
var str="flag="+document.getElementByIdx("DropDownList1").value;
xml.open("GET","showxml.aspx?"+str,false); //get和post都可以
xml.send();
return xml.responseText;
}
function dropdownlistclick()
{
var value=getAjaxXml();
window.open (value);
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" onchange="dropdownlistclick()">
<asp:ListItem Value="1.htm">1</asp:ListItem>
<asp:ListItem Value="2.htm">2</asp:ListItem>
<asp:ListItem Value="3.htm">3</asp:ListItem>
<asp:ListItem Value="4.htm">4</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
showxml.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _showxml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request ["flag"]);
}
}
相关文章推荐
- ie如何让阻止弹出框出现
- .net 如何弹出一个html页面。
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面[转载]
- .net如何实现页面间的参数传递
- JS中for循环出现的问题:如何给li元素绑定事件,点击每个li元素弹出对应的索引?
- 单页应用(SPA)前端javascript如何阻止按下返回键页面回退
- 如何在离开页面时弹出确认对话框
- .Net语言 APP开发平台——Smobiler学习日志:如何设置页面的title
- 如何在离开页面时弹出确认对话框
- [转]当用webBrowser1加载网页的时候,网页中出现js错误,会弹出一个提示,如何屏蔽不提示?
- 关闭页面代码和弹出窗口,出现确定,取消按钮+执行
- 如何调试、排错zen-cart出现空白页面(适用于1.3.9版)
- Android中如何阻止EditText自动弹出输入法
- .Net中,如何在页面上显示PDF文档
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 如何在IE, Firefox, Opera浏览器中支持页面弹出并回传数据
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 如何识别页面加载过程中出现的元素
- 框架结构下弹出框关闭后如何涮洗父页面
- 阻止因出现滚动条导致页面抖动