三级联动
数据库根据父级代号条件写查询 返回list<>集合
方法一:
创建三个下拉列表:
※AutoPostBack:否发生自动回传到服务器的操作。如果把该属性设置为 TRUE,则启用自动回传,否则为 FALSE。默认是 FALSE。
1 省:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList> 2 市:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList> 3 区:<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"></asp:DropDownList>
CS:
※SelectedIndexChanged事件:当列表控件的选定项在信息发往服务器之间变化时发生
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!IsPostBack) 4 { 5 Bind(new ChinaStatesDA().Select("0001"), DropDownList1); 6 Bind(new ChinaStatesDA().Select(DropDownList1.SelectedValue), DropDownList2); 7 Bind(new ChinaStatesDA().Select(DropDownList2.SelectedValue), DropDownList3); 8 } 9 DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged; 10 DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged; 11 } 12 13 void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 14 { 15 Bind(new ChinaStatesDA().Select(DropDownList2.SelectedValue), DropDownList3); 16 } 17 18 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 19 { 20 Bind(new ChinaStatesDA().Select(DropDownList1.SelectedValue), DropDownList2); 21 Bind(new ChinaStatesDA().Select(DropDownList2.SelectedValue), DropDownList3); 22 } 23 //绑定方法 24 public void Bind(List<ChinaStates> list, DropDownList dw) 25 { 26 dw.DataSource = list; 27 dw.DataTextField = "AreaName"; 28 dw.DataValueField = "AreaCode"; 29 dw.DataBind(); 30 }
方法二:
创建三个下拉列表框:
省:<asp:DropDownList ID="DropDownListsheng" runat="server" OnSelectedIndexChanged="DropDownListsheng_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> 市:<asp:DropDownList ID="DropDownListshi" runat="server" OnSelectedIndexChanged="DropDownListshi_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> 区:<asp:DropDownList ID="DropDownListqu" runat="server" AutoPostBack="True"></asp:DropDownList>
CS:
※DropDownList.Items.Clear(); 每调用一次填充方法就需要请空一下,否则数据会追加
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { sheng(); shi(); qu(); } } public void sheng()//填充省 { List<ChinaStates> listsheng = new ChinaStatesDA().Select("0001"); foreach (ChinaStates cssheng in listsheng) { ListItem lisheng = new ListItem(cssheng.AreaName, cssheng.AreaCode); DropDownListsheng.Items.Add(lisheng); } } public void shi()//填充市 { List<ChinaStates> listshi = new ChinaStatesDA().Select(DropDownListsheng.SelectedValue); foreach (ChinaStates csshi in listshi) { ListItem lishi = new ListItem(csshi.AreaName, csshi.AreaCode); DropDownListshi.Items.Add(lishi); } } public void qu()//填充区 { List<ChinaStates> listqu = new ChinaStatesDA().Select(DropDownListshi.SelectedValue); foreach (ChinaStates csqu in listqu) { ListItem liqu = new ListItem(csqu.AreaName, csqu.AreaCode); DropDownListqu.Items.Add(liqu); } } protected void DropDownListsheng_SelectedIndexChanged(object sender, EventArgs e) { DropDownListshi.Items.Clear(); DropDownListqu.Items.Clear(); shi(); qu(); } protected void DropDownListshi_SelectedIndexChanged(object sender, EventArgs e) { DropDownListqu.Items.Clear(); qu(); }