WebForm 三级联动

三级联动

 

数据库根据父级代号条件写查询 返回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();
    }

 

转载于:https://www.cnblogs.com/ShenG1/p/5895605.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值