Webfrom --中国直辖市三区联动

效果图:

(一)数据库操作方法:

public class mydb
{
    private MyDBDataContext context = new MyDBDataContext();
    public List<ChinaStates> Select(string AreaCode)//将中国的表全部查询
    {
        return  context.ChinaStates.Where(p=>p.ParentAreaCode==AreaCode).ToList();
    }

}
(二) 操作方法
public partial class yemian : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)//显示界面
    {
        if (!IsPostBack)
        {
            fangfa();
        }

  
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//根据第一个来操作第二个和第三个
    {
        string code = DropDownList1.SelectedItem.Value;
        List<ChinaStates> diqu1 = new mydb().Select(code);
        DropDownList2.DataSource = diqu1;
        DropDownList2.DataTextField = "AreaName";
        DropDownList2.DataValueField = "AreaCode";
        DropDownList2.DataBind();

        List<ChinaStates> diqu3= new mydb().Select(code);
        DropDownList3.DataSource = diqu3;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();

    }
    private void fangfa()//写了一个方法
    {
        List<ChinaStates> list = new mydb().Select("0001");
        DropDownList1.DataSource = list;//制定数据源
        DropDownList1.DataTextField = "AreaName";
        DropDownList1.DataValueField = "AreaCode";
        DropDownList1.DataBind();//最后绑定一下

        List<ChinaStates> shi = new mydb().Select("11");
        DropDownList2.DataSource = shi;
        DropDownList2.DataTextField = "AreaName";
        DropDownList2.DataValueField = "AreaCode";
        DropDownList2.DataBind();

        List<ChinaStates> diqu = new mydb().Select("1101");
        DropDownList3.DataSource = diqu;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();
    
    
    }

  protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)//根据第二个改变第三个
    {
        string code = DropDownList2.SelectedItem.Value;
        List<ChinaStates> diqu2 = new mydb().Select(code);
        DropDownList3.DataSource = diqu2;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();

    }
}


点击改变事件一定要将AutoPostBack 的属性改成true

 

转载于:https://www.cnblogs.com/w-wz/p/4658552.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值