2013 /3/11 省市县 三级联动 代码分享(*事先建好数据库)

后台代码:

    public partial class WebForm1 : System.Web.UI.Page
    {
        string constr = ConfigurationManager.ConnectionStrings["sqlPro"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 proBind();
            }
          
        }
        //首先将省份信息绑定到dropdownlist1 中
        private void proBind()
        {
            using (SqlConnection con=new SqlConnection(constr))
            {
                using (SqlCommand cmd=con.CreateCommand())
                {
                    con.Open();
                    cmd.CommandText = " select provinceID,province from province";
                    using (SqlDataAdapter adapter=new SqlDataAdapter(cmd))
                    {
                        DataTable dtPro = new DataTable();
                        adapter.Fill(dtPro);
                        this.DropDownList1.DataSource = dtPro;
                        this.DropDownList1.DataTextField = "province";
                        this.DropDownList1.DataValueField = "provinceID";
                        DropDownList1.DataBind();
                        DropDownList1.Items.Insert(0, "--请选择省份--");
                    }
                   
                }
            }
        }
//然后将城市信息绑定到 dropdownlist2中
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    con.Open();
                    int proId = Convert.ToInt32(DropDownList1.SelectedValue);
                    cmd.CommandText = " select cityID,city from city where father="+proId+"";
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        DataTable dtCity = new DataTable();
                        adapter.Fill(dtCity);
                        if (proId > 0)
                        {
                            this.DropDownList2.DataSource = dtCity;
                            this.DropDownList2.DataTextField = "city";
                            this.DropDownList2.DataValueField = "cityID";
                            DropDownList2.DataBind();
                            DropDownList2.Items.Insert(0,"-请选择城市-");
                        }
                        else
                        {
                            DropDownList2.Items.Clear();
                            DropDownList2.Items.Insert(0, new ListItem("--请选择城市--", "0"));
                            DropDownList2.Items.Clear();
                            DropDownList2.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                       
                        }
                      
                    }

                }
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    con.Open();
                    int cityId = Convert.ToInt32(DropDownList2.SelectedValue);
                    cmd.CommandText = " select areaID,area from area where father="+cityId+"";
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        DataTable dtCity = new DataTable();
                        adapter.Fill(dtCity);
                        if (cityId > 0)
                        {
                            this.DropDownList3.DataSource = dtCity;
                            this.DropDownList3.DataTextField = "area";
                            this.DropDownList3.DataValueField = "areaID";
                            DropDownList3.DataBind();
                            DropDownList3.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                        }
                        else
                        {
                            DropDownList3.Items.Clear();
                            DropDownList3.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                        }
                    }

                }
            }

        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string province = DropDownList1.SelectedItem.Text;
            string  city = DropDownList2.SelectedItem.Text;
            string area = DropDownList3.SelectedItem.Text;
            Label1.Text = province + city + area;
        }
       

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值