省市县三级连动

来源:互联网 发布:深圳恒汇众集团知乎 编辑:程序博客网 时间:2024/04/28 16:04

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace 省市县三级连动
{
    public partial class 省市县 : System.Web.UI.Page
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select provinceID,province from province";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            ddlsheng.DataSource = dt;
                            ddlsheng.DataTextField = "province";
                            ddlsheng.DataValueField = "provinceID";
                            ddlsheng.DataBind();
                            ddlsheng.Items.Insert(0, new ListItem("----请选择省份----", "0"));
                            ddlshi.Items.Insert(0, new ListItem("---请选择城市---", "0"));
                            ddlxian.Items.Insert(0, new ListItem("---请选择县---", "0"));
                        }
                       
                    }
                }
            }
        }

        protected void ddlsheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            string provinceID = ddlsheng.SelectedValue;
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select cityID,city from city where
father=@provinceid";
                    cmd.Parameters.Add(new SqlParameter("@provinceid",provinceID));
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
                        ddlshi.DataSource = dt;
                        ddlshi.DataTextField = "city";
                        ddlshi.DataValueField = "cityID";
                        ddlshi.DataBind();
                       
                    }
                   
                }
            }
        }

        protected void ddlshi_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cityID = ddlshi.SelectedValue;
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select areaID,area from area where
father=@cityid";
                    cmd.Parameters.Add(new SqlParameter("@cityid", cityID));
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    ddlxian.DataSource = dt;
                    ddlxian.DataValueField = "areaID";
                    ddlxian.DataTextField = "area";
                    ddlxian.DataBind();
                   
                    }
                   
                }
            }
        }
    }
}

原创粉丝点击