三级联动
数据库根据父级代号条件写查询 返回list集合
方法一:
创建三个下拉列表:
※AutoPostBack:否发生自动回传到服务器的操作。如果把该属性设置为 TRUE,则启用自动回传,否则为 FALSE。默认是 FALSE。
1 省:
2 市:
3 区:
CS:
※SelectedIndexChanged事件:当列表控件的选定项在信息发往服务器之间变化时发生
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 Bind(new ChinaStatesDA().Select("0001"//代码效果参考:http://www.lyjsj.net.cn/wx/art_23086.html
), 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 list, DropDownList dw)
25 {
26 dw.DataSource = list;
27 dw.DataTextField = "AreaName";
28 dw.DataValueField = "AreaCode";
29 dw.DataBind();
30 }
方法二:
创建三个下拉列表框:
省:
市:
区:
CS:
※DropDownList.Items.Clear(); 每调用一次填充方法就需要请空一下,否则数据会追加
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sheng();
shi();
qu();
}
}
public void sheng()//填充省
{
List 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 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 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();
}