数据模型:
1 public class Model 2 { 3 public Model() 4 { 5 // 6 // TODO: 在此处添加构造函数逻辑 7 // 8 } 9 /// <summary> 10 /// 地区编号 11 /// </summary> 12 public string AreaCode 13 { 14 get { return _AreaCode; } 15 set { _AreaCode = value; } 16 } 17 private string _AreaName; 18 /// <summary> 19 /// 地区名字 20 /// </summary> 21 public string AreaName 22 { 23 get { return _AreaName; } 24 set { _AreaName = value; } 25 } 26 private string _ParentAreaCode; 27 /// <summary> 28 /// 地区父地区编号 29 /// </summary> 30 public string ParentAreaCode 31 { 32 get { return _ParentAreaCode; } 33 set { _ParentAreaCode = value; } 34 } 35 36 }
数据访问类:
1 public class Operation 2 { 3 SqlConnection conn = null; 4 SqlCommand com = null; 5 public Operation() 6 { 7 conn = new SqlConnection("server=.;database=ChangYong;user=sa;pwd=123;"); 8 com = conn.CreateCommand(); 9 } 10 /// <summary> 11 /// 查询中国所有信息 12 /// </summary> 13 /// <returns></returns> 14 public List<Model> selectChina(string code) 15 { 16 List<Model> list = new List<Model>(); 17 com.CommandText = "select * from ChinaStates where ParentAreaCode = @code"; 18 com.Parameters.Clear(); 19 com.Parameters.Add("@code", code); 20 conn.Open(); 21 SqlDataReader dr = com.ExecuteReader(); 22 if (dr.HasRows) 23 { 24 while (dr.Read()) 25 { 26 Model M = new Model(); 27 M.AreaCode = dr["AreaCode"].ToString(); 28 M.AreaName = dr["AreaName"].ToString(); 29 list.Add(M); 30 } 31 } 32 conn.Close(); 33 return list; 34 }
页面html代码:
1 <body> 2 <form id="form1" runat="server"> 3 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList> 4 <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList> 5 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList> 6 </form> 7 </body>
后台代码:
1 List<Model> list = null; 2 protected void Page_Load(object sender, EventArgs e) 3 { 4 DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//省改变市自动变 5 DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;//市改变区自动变 6 if (IsPostBack == false) 7 { 8 list = new Operation().selectChina("0001");//添加省 9 DropDownList1.DataSource = list; 10 DropDownList1.DataTextField = "AreaName"; 11 DropDownList1.DataValueField = "AreaCode"; 12 DropDownList1.DataBind(); 13 list = new Operation().selectChina(DropDownList1.SelectedItem.Value);//添加市 14 DropDownList2.DataSource = list; 15 DropDownList2.DataTextField = "AreaName"; 16 DropDownList2.DataValueField = "AreaCode"; 17 DropDownList2.DataBind(); 18 list = new Operation().selectChina(DropDownList2.SelectedItem.Value);//添加区 19 DropDownList3.DataSource = list; 20 DropDownList3.DataTextField = "AreaName"; 21 DropDownList3.DataValueField = "AreaCode"; 22 DropDownList3.DataBind(); 23 } 24 } 25 26 void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 27 { 28 list = new Operation().selectChina(DropDownList2.SelectedItem.Value); 29 DropDownList3.DataSource = list; 30 DropDownList3.DataTextField = "AreaName"; 31 DropDownList3.DataValueField = "AreaCode"; 32 DropDownList3.DataBind(); 33 } 34 35 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 36 { 37 list = new Operation().selectChina(DropDownList1.SelectedItem.Value); 38 DropDownList2.DataSource = list; 39 DropDownList2.DataTextField = "AreaName"; 40 DropDownList2.DataValueField = "AreaCode"; 41 DropDownList2.DataBind(); 42 list = new Operation().selectChina(DropDownList2.SelectedItem.Value); 43 DropDownList3.DataSource = list; 44 DropDownList3.DataTextField = "AreaName"; 45 DropDownList3.DataValueField = "AreaCode"; 46 DropDownList3.DataBind(); 47 }
效果就是DropDownList1选中的项发生变化时DropDownList2和DropDownList3选中的项会自动根据数据库中所建立好的关系进行变化,
DropDownList2选中的项发生变化时DropDownList3选中的项会自动根据数据库中所建立好的关系进行变化。