省级三连动(二)

前台:

<div>
      <asp:DropDownList ID="DropDownList1" runat="server" Height="29px" 
            Width="150px" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged1" 
            AutoPostBack="True">
            <asp:ListItem Value="0">----请选择省份----</asp:ListItem>
        </asp:DropDownList>
&nbsp;
        <asp:DropDownList ID="DropDownList2" runat="server" Height="29px" Width="135px" 
            AutoPostBack="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem Value="0">----请选择市----</asp:ListItem>
        </asp:DropDownList>
    &nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList3" runat="server" Height="29px" Width="135px">
            <asp:ListItem Value="0">----请选县----</asp:ListItem>
        </asp:DropDownList>
    </div>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace 省级连动
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDownList();
            }
        }
        private void FillDownList()
        {
            string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from S_Province";

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.DropDownList1.DataSource = reader;
                        this.DropDownList1.DataTextField = "ProvinceName";
                        this.DropDownList1.DataValueField = "ProvinceID";
                        this.DropDownList1.DataBind();
                    }
                }

            }
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select CityID,CityName from S_City where ProvinceID=@xxx";
                    SqlParameter paran = new SqlParameter("xxx", this.DropDownList1.SelectedValue);
                    cmd.Parameters.Add(paran);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.DropDownList2.DataSource = reader;
                        this.DropDownList2.DataTextField = "CityName";
                        this.DropDownList2.DataValueField = "CityID";
                        this.DropDownList2.DataBind();
                    }
                }
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select DistrictID,DistrictName from S_District where CityID=@xxx";
                    SqlParameter paran = new SqlParameter("xxx", this.DropDownList2.SelectedValue);
                    cmd.Parameters.Add(paran);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.DropDownList3.DataSource = reader;
                        this.DropDownList3.DataTextField = "DistrictName";
                        this.DropDownList3.DataValueField = "DistrictID";
                        this.DropDownList3.DataBind();
                    }
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/duanlinlin/p/2954621.html