服務(wù)熱線
153 8323 9821
一.用Response.Redirect()
比如在這個(gè)頁(yè)面中有一個(gè)DropDownList 和 一個(gè)button ,dropdownlist 的value為一些頁(yè)面的URL,當(dāng)選好后,點(diǎn)擊Button后,轉(zhuǎn)向dropdownlist 的value對(duì)應(yīng)的頁(yè)面。
代碼如下:
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實(shí)現(xiàn)
3、 問(wèn)題搞清楚了事情也就好辦了,我們可以用ajax來(lái)與服務(wù)器端通信,在客戶端用javascript來(lái)編寫(xiě)事件處理程序,這樣在客戶端的就不會(huì)因?yàn)榛貍魇录匦录虞d頁(yè)面,這樣彈出窗口就不會(huì)被阻止了!
如:
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>無(wú)標(biāo)題頁(yè)</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"]);
}
}