無重新整理三級聯動,這個在網上有很多,方法也很好。但是有一點比較特別的就是我的這個,是因為單位已經建立好了資料庫,你得在這個上面進行添加。所以很麻煩啊。在網上也找了一些,可是大多數資料庫都不一樣,有一些是用vs2005和ajaxpro做的,可是我單位使用的是vs2003沒有法子,只能找一些可以用了。這裡要感謝的是 Eric 它在baidu的BLOG裡的方法不錯,我基本是採用它的方法來做的,只不過這裡做了一些改動。
首先是。ThreeDDL.aspx頁面,我把程式全寫上來,
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
<script language="javascript">
<!--
function SelectProvince()
{
var drp2=document.getElementById("DropDownList2");//市縣一級
var drp3=document.getElementById("DropDownList3");//區一級
for (var i=drp2.options.length;i>=0;i--)
{
drp2.remove(i);
}
for (var i=drp3.options.length;i>=0;i--)
{
drp3.remove(i);
}
var oHttpReq=new ActiveXObject("MSXML2.XMLHTTP");
var oDoc=new ActiveXObject("MSXML2.DOMDocument");
var province=document.getElementById("DropDownList1").value;
oHttpReq.open("post","selectarea.aspx?flag=1&province="+province,false);
oHttpReq.setrequestheader("content-type","application/x-www-form-urlencoded");
oHttpReq.send("");
var result=oHttpReq.responseText;
var newOption_0 = document.createElement("OPTION");
newOption_0.text = "-請選擇-";
newOption_0.value = '-1';
drp2.options.add(newOption_0);
oDoc.loadXML(result);
//這裡要說一下,items1和items2是你在後面程式中得到的一個xml表。對於//NewDataSet/city/name是怎麼來的,你可以用alert(result);看一下就行
items1 = oDoc.selectNodes("//NewDataSet/city/name");
items2 = oDoc.selectNodes("//NewDataSet/city/sz_code");
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++)
//將小類的類名和編號賦予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
function SelectCity()
{
var drp3=document.getElementById("DropDownList3");
for (var i=drp3.options.length;i>=0;i--)
{
drp3.remove(i);
}
var oHttpReq=new ActiveXObject("MSXML2.XMLHTTP");
var oDoc=new ActiveXObject("MSXML2.DOMDocument");
var city=document.getElementById("DropDownList2").value;
oHttpReq.open("post","selectarea.aspx?flag=2&city="+city,false);
oHttpReq.setrequestheader("content-type","application/x-www-form-urlencoded");
oHttpReq.send("");
var result=oHttpReq.responseText;
var newOption_0 = document.createElement("OPTION");
newOption_0.text = "-請選擇-";
newOption_0.value = '-1';
drp3.options.add(newOption_0);
oDoc.loadXML(result);
var items = oDoc.selectNodes("//NewDataSet/area");
for (var item = items.nextNode();item;item = items.nextNode())
{
var areaName = item.selectSingleNode("name").nodeTypedValue;
var areaCode = item.selectSingleNode("sz_code").nodeTypedValue;
var newOption = document.createElement("OPTION");
newOption.text = areaName;
newOption.value = areaCode;
drp3.options.add(newOption);
}
}
-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList></div>
</form>
</body>
</html>
下面是ThreeDDl.aspx.cs的代碼
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=CG;Initial Catalog=area;User ID=sa;Password=chen123");
SqlDataAdapter da = new SqlDataAdapter("select * from area where right(sz_code,4)='0000'",conn);
DataSet ds = new DataSet();
da.Fill(ds, "Province");
this.DropDownList1.DataSource = ds.Tables["Province"];
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "sz_code";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange", "SelectProvince()");
this.DropDownList2.Attributes.Add("onchange", "SelectCity()");
}
selectarea.aspx這個頁面上面沒有什麼東西,只有在其selectarea.aspx.cs中才有代碼,在下面
using System;
using System.Data;
using System.Data.SqlClient;
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;
using System.Xml;
public partial class selectarea : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=CG;Initial Catalog=area;User ID=sa;Password=chen123");
int flag = Convert.ToInt32(Request.QueryString["flag"].ToString());
if (Request.QueryString["flag"] != null || Request.QueryString["flag"].ToString() != "")
{
switch(flag)
{
case 1:
string strprv = Request.QueryString["province"].ToString();
SqlDataAdapter da = new SqlDataAdapter("select * from area where left(sz_code,2)='" + strprv.Substring(0, 2).ToString() + "' and right(sz_code,2)='00' and right(sz_code,4)<>'0000'", conn);
DataSet ds = new DataSet();
da.Fill(ds,"city");
XmlTextWriter writerCity = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writerCity.Formatting = Formatting.Indented;
writerCity.Indentation = 4;
writerCity.IndentChar = ' ';
ds.WriteXml(writerCity);
writerCity.Flush();
Response.End();
writerCity.Close();
break;
case 2:
string strcity = Request.QueryString["city"].ToString();
SqlDataAdapter da1 = new SqlDataAdapter("select * from area where left(sz_code,4)='" + strcity.Substring(0, 4).ToString() + "' and right(sz_code,2)<>'00'", conn);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "area");
XmlTextWriter writerCity1 = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writerCity1.Formatting = Formatting.Indented;
writerCity1.Indentation = 4;
writerCity1.IndentChar = ' ';
ds1.WriteXml(writerCity1);
writerCity1.Flush();
Response.End();
writerCity1.Close();
break;
}
}
}
}
這裡我把資料庫的大概情況給大家發一下,如果想要資料庫ftp://down:down@ftp.netc.hutc.zj.cn/city.rar這個是Eric朋友提供的下載我不知道能用多長時間,我單位的資料庫與它差不多,因為我無法提供我的資料庫,所以以上代碼全是根據它的資料庫格式寫的。
表名area
id name Rome sz_code zm_code
1 北京 Beijing Shi 110000 BJ
這裡說一下,省一級的包括北京,天津這樣的獨立的市,sz_code全是前二位有數,後四位全是0000
而下面一級的,市縣一級的則是前二位與省級一樣,中間二位不同,後二位是00
而最後一級,區一級的,前二位與省級一樣,中間與市級一樣,最後二位是自己的號。