这类东西很多人都写过,但我是第一次写,支持一下菜鸟的原创吧~
第一级是写死的(因为省不会变的),以下三级都是现查的数据库,由于没有采用DIV格式,所以有些ID很长~
js代码:
1function ProvinceChange(obj1,obj2,obj3)
    2 {
    3        if(typeof(ActiveXObject) != "undefined")
    4        {
    5                if(obj1.value=="")
    6                {
    7                        obj2.length=0;
    8                        obj3.length=0;
    9                        form1.Wizard1_SchoolAddForm1_District.length=0;
10                }
11                else
12                {
13                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
14                        var item = obj1.options[obj1.selectedIndex].value;
15                        var ReqStr = '<?xml version="1.0"?>';                    
16                        xmlHttp.open("GET","SettleForm.aspx?province="+item);
17                        xmlHttp.send(ReqStr);
18                        xmlHttp.onreadystatechange = HandleResult;
19                }
20        }
21        function HandleResult()
22        {
23             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
24             {
25                        obj2.length=0;
26                        obj3.length=0;
27                        form1.Wizard1_SchoolAddForm1_District.length=0;
28                        obj2.options[obj2.length]=new Option("");
29                        obj3.options[obj3.length]=new Option("");
30                        var e=xmlHttp.responseText;
31                        alert(e);
32                        if(e!="未添加")
33                        {
34                                var a=e.split("*");
35                                var arr1=a[0].toString().split(",");
36                                var arr2=a[1].toString().split(",");
37                                for(var i=0;i<arr1.length;i++)
38                                {                                                
39                                        obj2.options[obj2.length]=new Option(arr1[i]);
40                                }
41                                for(var j=0;j<arr2.length;j++)
42                                {                                                
43                                        obj3.options[obj3.length]=new Option(arr2[j]);
44                                }
45                        }
46             }
47        }
48 }
49    
50 function CityChange(obj1,obj2,obj3)
51 {
52        if(typeof(ActiveXObject) != "undefined")
53        {
54                if(obj1.selectedIndex==0)
55                {
56                         obj2.length=0;
57                         obj3.length=0;
58                }
59                else
60                {
61                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
62                        var ReqStr = '<?xml version="1.0"?>';    
63                        var item = obj1.options[obj1.selectedIndex].text;                
64                        xmlHttp.open("GET","SettleForm.aspx?city="+item);
65                        xmlHttp.send(ReqStr);
66                        xmlHttp.onreadystatechange = HandleResult;
67                }
68        }
69        function HandleResult()
70        {
71             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
72             {
73                        obj2.length=0;
74                        obj2.options[obj2.length]=new Option("");
75                        obj3.length=0;
76                        obj3.options[obj3.length]=new Option("");
77                        var e=xmlHttp.responseText;
78                        if(e!="未添加")
79                        {
80                                var a=e.split("*");
81                                var arr1=a[0].toString().split(",");
82                                var arr2=a[1].toString().split(",");
83                                for(var i=0;i<arr1.length;i++)
84                                {                                                
85                                        obj2.options[obj2.length]=new Option(arr1[i]);
86                                }
87                                for(var j=0;j<arr2.length;j++)
88                                {                                                
89                                        obj3.options[obj3.length]=new Option(arr2[j]);
90                                }
91                        }    
92             }
93        }
94    
95 }
96    
97 function DistrictChange(obj1,obj2)
98 {
99        if(typeof(ActiveXObject) != "undefined")
100        {
101                if(obj1.selectedIndex==0)
102                {
103                         obj2.length=0;
104                }
105                else
106                {
107                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
108                        var item = obj1.options[obj1.selectedIndex].text;
109                        var ReqStr = '<?xml version="1.0"?>';                    
110                        xmlHttp.open("GET","SettleForm.aspx?district="+item);
111                        xmlHttp.send(ReqStr);
112                        xmlHttp.onreadystatechange = HandleResult;
113                }
114        }
115        function HandleResult()
116        {
117             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
118             {
119                        obj2.length=0;
120                        obj2.options[obj2.length]=new Option("");
121                        if(e!="未添加")
122                        {
123                                var e=xmlHttp.responseText;
124                                var arr=e.split(",")
125                                for(var i=0;i<arr.length;i++)
126                                {                                                
127                                        obj2.options[obj2.length]=new Option(arr[i]);
128                                }
129                        }
130             }
131        }
132 }
133
 
中间页代码:
Ajax实现联动效果_联动public partial class BusinessmanManage_SettleForm : System.Web.UI.Page
Ajax实现联动效果_联动 19{
Ajax实现联动效果_联动 20        protected ShoppingChain.BLL.Product p = new ShoppingChain.BLL.Product();
Ajax实现联动效果_联动 21        protected ShoppingChain.BLL.Register r = new ShoppingChain.BLL.Register();
Ajax实现联动效果_联动 22        protected void Page_Load(object sender, EventArgs e)
Ajax实现联动效果_联动 23        {
Ajax实现联动效果_联动 24                        if (Request.QueryString["province"] != null)
Ajax实现联动效果_联动 25                {
Ajax实现联动效果_联动 26                        IList<SchoolInfo> list1 = r.GetCityByProvince(Request.QueryString["province"]);
Ajax实现联动效果_联动 27                        IList<SchoolInfo> list2 = r.GetSchoolByProvince(Request.QueryString["province"]);
Ajax实现联动效果_联动 28                        if (list1.Count == 0)
Ajax实现联动效果_联动 29                        {
Ajax实现联动效果_联动 30                                Response.Write("未添加");
Ajax实现联动效果_联动 31                        }
Ajax实现联动效果_联动 32                        else
Ajax实现联动效果_联动 33                        {
Ajax实现联动效果_联动 34                                //string cityshort = "";
Ajax实现联动效果_联动 35                                string cityname = "";
Ajax实现联动效果_联动 36                                string school = "";
Ajax实现联动效果_联动 37                                foreach (SchoolInfo info in list1)
Ajax实现联动效果_联动 38                                {
Ajax实现联动效果_联动 39                                        cityname = cityname + info.City + ",";
Ajax实现联动效果_联动 40                                }
Ajax实现联动效果_联动 41                                //string str = cityshort.Remove(cityshort.Length - 1);
Ajax实现联动效果_联动 42                                string strcname = cityname.Remove(cityname.Length - 1);
Ajax实现联动效果_联动 43                                foreach (SchoolInfo info in list2)
Ajax实现联动效果_联动 44                                {
Ajax实现联动效果_联动 45                                        school = school + info.School + ",";
Ajax实现联动效果_联动 46                                }
Ajax实现联动效果_联动 47                                string strschool = school.Remove(school.Length - 1);
Ajax实现联动效果_联动 48                                string s = strcname + "*" + strschool;
Ajax实现联动效果_联动 49                                Response.Write(s);
Ajax实现联动效果_联动 50                        }
Ajax实现联动效果_联动 51                }
Ajax实现联动效果_联动 52                if (Request.QueryString["city"] != null)
Ajax实现联动效果_联动 53                {
Ajax实现联动效果_联动 54                        IList<SchoolInfo> list1 = r.GetDistrictByCity(Request.QueryString["city"]);
Ajax实现联动效果_联动 55                        IList<SchoolInfo> list2 = r.GetSchoolByCity(Request.QueryString["city"]);
Ajax实现联动效果_联动 56                        string district = "";
Ajax实现联动效果_联动 57                        string school = "";
Ajax实现联动效果_联动 58                        if (list1.Count == 0)
Ajax实现联动效果_联动 59                        {
Ajax实现联动效果_联动 60                                Response.Write("未添加");
Ajax实现联动效果_联动 61                        }
Ajax实现联动效果_联动 62                        else
Ajax实现联动效果_联动 63                        {
Ajax实现联动效果_联动 64                                foreach (SchoolInfo info in list1)
Ajax实现联动效果_联动 65                                {
Ajax实现联动效果_联动 66                                        district = district + info.District + ",";
Ajax实现联动效果_联动 67                                }
Ajax实现联动效果_联动 68                                string strdistrict = district.Remove(district.Length - 1);
Ajax实现联动效果_联动 69
Ajax实现联动效果_联动 70                                foreach (SchoolInfo info in list2)
Ajax实现联动效果_联动 71                                {
Ajax实现联动效果_联动 72                                        school = school + info.School + ",";
Ajax实现联动效果_联动 73                                }
Ajax实现联动效果_联动 74                                string strschool = school.Remove(school.Length - 1);
Ajax实现联动效果_联动 75
Ajax实现联动效果_联动 76                                string s = strdistrict + "*" + strschool;
Ajax实现联动效果_联动 77                                Response.Write(s);
Ajax实现联动效果_联动 78                        }
Ajax实现联动效果_联动 79                }
Ajax实现联动效果_联动 80                if (Request.QueryString["district"] != null)
Ajax实现联动效果_联动 81                {
Ajax实现联动效果_联动 82                        IList<SchoolInfo> list = r.GetSchoolByDistrict(Request.QueryString["district"]);
Ajax实现联动效果_联动 83                        if (list.Count == 0)
Ajax实现联动效果_联动 84                        {
Ajax实现联动效果_联动 85                                Response.Write("未添加");
Ajax实现联动效果_联动 86                        }
Ajax实现联动效果_联动 87                        else
Ajax实现联动效果_联动 88                        {
Ajax实现联动效果_联动 89                                string schoolname = "";
Ajax实现联动效果_联动 90                                foreach (SchoolInfo info in list)
Ajax实现联动效果_联动 91                                {
Ajax实现联动效果_联动 92                                        schoolname = schoolname + info.School + ",";
Ajax实现联动效果_联动 93                                }
Ajax实现联动效果_联动 94                                string strschool = schoolname.Remove(schoolname.Length - 1);
Ajax实现联动效果_联动 95                                Response.Write(strschool);
Ajax实现联动效果_联动 96                        }
Ajax实现联动效果_联动 97                }
Ajax实现联动效果_联动 98        }
Ajax实现联动效果_联动 99}