前台:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="省市县.aspx.cs" Inherits="省级连动.省市县" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlProvince" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlProvince_SelectedIndexChanged">
        <asp:ListItem Value="0">--省份--</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlCity_SelectedIndexChanged">
        <asp:ListItem Value="0">--城市--</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddlDistrict" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlDistrict_SelectedIndexChanged">
        <asp:ListItem Value="0">--县区--</asp:ListItem>
        </asp:DropDownList>


    </div>
    </form>
</body>

</html>

后台:

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;
using System.Data;
using System.Configuration;


namespace 省级连动
{
    public partial class 省市县 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                getddlProvinceDataBind();       //页面首次加载执行省份绑定
            }
        }


        public void getddlProvinceDataBind()
        {
            string sqlProvince = "select * from S_Province";
            ddlProvince.DataSource = getDataSet(sqlProvince);
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();
            ddlProvince.Items.Insert(0, new ListItem("--省份--", "0"));
        }


        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
            if (ProvinceID > 0)
            {
                string sqlCity = "select * from S_City WHERE ProvinceID=" + ProvinceID + "";       //根据省份ID找城市
                ddlCity.DataSource = getDataSet(sqlCity);
                ddlCity.DataTextField = "CityName";
                ddlCity.DataValueField = "CityID";
                ddlCity.DataBind();
                ddlCity.Items.Insert(0, new ListItem("--请选择城市--", "0"));
            }
            else
            {
                ddlCity.Items.Clear();
                ddlCity.Items.Insert(0, new ListItem("--请选择城市--", "0"));
                ddlDistrict.Items.Clear();
                ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
            }
        }
    
        


        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CityID = Convert.ToInt32(ddlCity.SelectedValue);
            if (CityID > 0)
            {
                string sqlDistrict = "SELECT * FROM S_District WHERE CityID=" + CityID + "";       //根据城市ID找县区
                ddlDistrict.DataSource = getDataSet(sqlDistrict);
                ddlDistrict.DataTextField = "DistrictName";
                ddlDistrict.DataValueField = "DistrictID";
                ddlDistrict.DataBind();
                ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
            }
            else
            {
                ddlDistrict.Items.Clear();
                ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
            }
        }


        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
            int CityID = Convert.ToInt32(ddlCity.SelectedValue);
            int DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);
        }
        public DataSet getDataSet(string sql)       
        {
            string conStr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
            SqlConnection conn = new SqlConnection(conStr);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
    }
}

posted on 2013-03-11 20:02  小菜鸟——  阅读(152)  评论(0编辑  收藏  举报