前台代码: < head runat = " server " > < title > asp.net实现DropDownList、ListBox无刷新三级联动的两种方法 </ title > < SCRIPT LANGUAGE = " JavaScript " > <!-- // 以XML求取ListBox2的数据 function XmlPost2(obj) ... { var svalue = obj.value; var webFileUrl = "?povinceid=" + svalue; var result = ""; var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); xmlHttp.open("POST", webFileUrl, false); xmlHttp.send(""); result = xmlHttp.responseText; if(result != "") ...{ document.all("ListBox2").length=0; var piArray = result.split(",");//两次分割字符串来实现 for(var i=0;i<piArray.length;i++) ...{ var ary1 = piArray[i].toString().split("|"); document.all("ListBox2").options.add(new Option(ary1[1].toString(),ary1[0].toString())); } } else ...{ alert(result); } } // 以XML求取ListBox3的数据 function XmlPost3(obj) ... { var svalue = obj.value; var webFileUrl = "?cityid=" + svalue; var result = ""; var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); xmlHttp.open("POST", webFileUrl, false); xmlHttp.send(""); result = xmlHttp.responseText; if(result != "") ...{ document.all("ListBox3").length=0; var piArray = result.split(","); for(var i=0;i<piArray.length;i++) ...{ var ary1 = piArray[i].toString().split("|"); document.all("ListBox3").options.add(new Option(ary1[1].toString(),ary1[0].toString())); } } else ...{ alert(result); } } // --> </ SCRIPT > </ head > < body > < form id = " Form1 " method = " post " runat = " server " > < asp:ListBox id = " ListBox1 " style = " Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 25px " runat = " server " Height = " 432px " Width = " 168px " ></ asp:ListBox > < asp:ListBox id = " ListBox2 " style = " Z-INDEX: 102; LEFT: 271px; POSITION: absolute; TOP: 23px " runat = " server " Height = " 432px " Width = " 168px " ></ asp:ListBox > < asp:ListBox id = " ListBox3 " style = " Z-INDEX: 103; LEFT: 508px; POSITION: absolute; TOP: 25px " runat = " server " Height = " 432px " Width = " 168px " ></ asp:ListBox > </ form > </ body > 后台代码: DB cn = new DB(); Page_Load #region Page_Load private void Page_Load(object sender, System.EventArgs e) ...{ if (!this.IsPostBack) ...{ this.down1_bind(); this.ListBox1.Attributes.Add("onchange", "XmlPost2(this);"); this.ListBox2.Attributes.Add("onchange", "XmlPost3(this);"); } if (povinceid != "") ...{ this.down2_bind(povinceid); } if (cityid != "") ...{ this.down3_bind(cityid); } } #endregion property #region property private string povinceid ...{ get ...{ if (ViewState["povinceid"] != null && ViewState["povinceid"].ToString() != "") ...{ return ViewState["povinceid"].ToString(); } else ...{ if (Request["povinceid"] != null && Request["povinceid"].ToString() != "") ...{ return Request["povinceid"]; } else ...{ return ""; } } } set ...{ ViewState["povinceid"] = value; } } private string cityid ...{ get ...{ if (ViewState["cityid"] != null && ViewState["cityid"].ToString() != "") ...{ return ViewState["cityid"].ToString(); } else ...{ if (Request["cityid"] != null && Request["cityid"].ToString() != "") ...{ return Request["cityid"]; } else ...{ return ""; } } } set ...{ ViewState["povinceid"] = value; } } #endregion down2_bind #region down2_bind private void down2_bind(string id) ...{ string mystr = ""; string sql = "select cityID,city from city where father = '" + id + "'"; DataTable dt = cn.ReturnTable2(sql); if (dt.Rows.Count != 0) ...{ for (int i = 0; i < dt.Rows.Count; i++) ...{ mystr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString(); } mystr = mystr.Substring(1); } this.Response.Write(mystr); this.Response.End(); } #endregion down3_bind #region down3_bind private void down3_bind(string id) ...{ string mystr = ""; string sql = "select areaID,area from area where father = '" + id + "'"; DataTable dt = cn.ReturnTable2(sql); if (dt.Rows.Count != 0) ...{ for (int i = 0; i <dt.Rows.Count; i++) ...{ mystr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString(); } mystr = mystr.Substring(1); } this.Response.Write(mystr); this.Response.End(); } #endregion down1_bind