三级联动
新建ChinaStates类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 /// <summary> 7 /// ChinaStatesDA 的摘要说明 8 /// </summary> 9 public class ChinaStatesDA 10 { 11 private DataClassesDataContext Context; 12 public ChinaStatesDA() 13 { 14 // 15 // TODO: 在此处添加构造函数逻辑 16 // 17 Context = new DataClassesDataContext(); 18 } 19 public List<ChinaStates> select(string parentcode) 20 { 21 return Context.ChinaStates.Where(r=>r.ParentAreaCode==parentcode).ToList(); 22 } 23 }
后端代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class Default2 : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 if (!IsPostBack) 13 { 14 binddl(DropDownList1,"0001"); 15 } 16 } 17 private void binddl(DropDownList ddl, string code) 18 { 19 ddl.Items.Clear(); 20 ddl.Items.Add("--请选择--"); 21 foreach (ChinaStates data in new ChinaStatesDA().select(code)) 22 { 23 ListItem it = new ListItem(data.AreaName, data.AreaCode); 24 ddl.Items.Add(it); 25 } 26 } 27 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 28 { 29 binddl(DropDownList2, DropDownList1.SelectedValue); ; 30 } 31 protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 32 { 33 binddl(DropDownList3,DropDownList2.SelectedValue); 34 } 35 }