Asp.Net中DropDownList控件实现省市级联动

参考资料:https://blog.csdn.net/liu_111111/article/details/8243567

数据库准备

从左到右分别为[pro]表(省)、[city]表(市)、[dis]表(区)。

前端代码

<asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Label runat="server" Text="选择省:"></asp:Label>
                    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>
                    <asp:Label runat="server" Text="&nbsp;| 选择市:"></asp:Label>
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
                    </asp:DropDownList>
                    <asp:Label runat="server" Text="&nbsp;| 选择区:"></asp:Label>
                    <asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>
                    <br />
                    <asp:Label ID="x1" runat="server" Text="Label"></asp:Label><%--显示所选省份--%>
            &nbsp;|
            <asp:Label ID="x2" runat="server" Text="Label"></asp:Label><%--显示所选市--%>
            &nbsp;|
            <asp:Label ID="x3" runat="server" Text="Label"></asp:Label><%--显示所选区--%>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><%--测试局部刷新--%>

后台代码

        public static SqlConnection DBConn()
        {
            return new SqlConnection("server=;database=;uid=;pwd=");//连接数据库
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!this.IsPostBack)//第一次进入页面
            {
                bindpro();
            }
        }
        private void pro()//读取省
        {
            SqlConnection conn = DBConn();
            string sqlpro = "select pro_id,pro_name from [pro] ";
            SqlCommand procmd = new SqlCommand(sqlpro, conn);
            SqlDataAdapter myda0 = new SqlDataAdapter(procmd);
            DataTable mydt0 = new DataTable();
            myda0.Fill(mydt0);
            DropDownList1.DataSource = mydt0;
            DropDownList1.DataTextField = "pro_name";
            DropDownList1.DataValueField = "pro_id";
            DropDownList1.DataBind();

        }
        private void city()//读取市
        {
            SqlConnection conn = DBConn();
            string sqlcity = "select city_name,city_id from [city] where pro_id = '" + DropDownList1.SelectedValue + "'";
            SqlCommand citycmd = new SqlCommand(sqlcity, conn);
            SqlDataAdapter myda = new SqlDataAdapter(citycmd);
            DataTable mydt = new DataTable();
            myda.Fill(mydt);
            DropDownList2.DataSource = mydt;
            DropDownList2.DataValueField = "city_id";
            DropDownList2.DataTextField = "city_name";
            DropDownList2.DataBind();

        }
        private void dis()//读取区
        {
            SqlConnection conn = DBConn();
            string sqldis = "select dis_name,dis_id from [dis] where city_id = '" + DropDownList2.SelectedValue + "'";
            SqlCommand discmd = new SqlCommand(sqldis, conn);
            SqlDataAdapter myda1 = new SqlDataAdapter(discmd);
            DataTable mydt1 = new DataTable();
            myda1.Fill(mydt1);
            DropDownList3.DataSource = mydt1;
            DropDownList3.DataValueField = "dis_id";
            DropDownList3.DataTextField = "dis_name";
            DropDownList3.DataBind();
        }

        private void bindpro()//第一次进入页面时加载所有省市区第一位数据
        {
            pro();
            city();
            dis();
            x1.Text = DropDownList1.SelectedItem.Text;
            x2.Text = DropDownList2.SelectedItem.Text;
            x3.Text = DropDownList3.SelectedItem.Text;
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//选择新省
        {
            city();
            dis();
            x1.Text = DropDownList1.SelectedItem.Text;
            x2.Text = DropDownList2.SelectedItem.Text;
            x3.Text = DropDownList3.SelectedItem.Text;
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)//选择新市
        {
            dis();
            x2.Text = DropDownList2.SelectedItem.Text;
            x3.Text = DropDownList3.SelectedItem.Text;
        }
        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)选择新区
        {
            x3.Text = DropDownList3.SelectedItem.Text;
        }

一些问题:你可能注意到在pro()、city()、dis()中并没有使用conn.Open();和conn.Close();,但是这却能成功运行,查阅资料后我发现原因在于SqlDataAdapter是自动打开自动释放的。不过我想使用Open()和Close()应该是一个好的习惯吧。

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值