vs2005使用DropDownList实现省份城市的联动

首先创建一个数据库,执行sql语句:

create database dropDownTest

use dropDownTest

create table province
(
     proId int primary key,
     proName varchar(50) not null
)

insert into province values (1,'北京')
insert into province values (2,'山东')
insert into province values (3,'辽宁')
insert into province values (4,'江苏')
insert into province values (5,'河北')
insert into province values (6,'浙江')

create table City
(
     cityID int primary key ,
     proID  int foreign  key  references province (proID),   //关联外键省份ProID
     cityName varchar (50) not null
)

insert into city values (1,1,'北京')
insert into city values (2,2,'济南')
insert into city values (3,2,'青岛')
insert into city values (4,2,'烟台')
insert into city values (5,3,'大连')
insert into city values (6,3,'沈阳')
insert into city values (7,4,'南京')
insert into city values (8,5,'石家庄')

数据库创建完 了,下面我们开始写代码,很简单的

首先创建一个连接数据库类db.cs

using System.Data.SqlClient;

 public static SqlConnection Createconnection()
    {
        SqlConnection con = new SqlConnection("server=.;database=dropdowntest;uid=sa;pwd=sa;");
        return con;
    }

在代码中加入以上代码,实现数据库连接

页面设计代码托进两个dropdownlist 控件,我就直接贴aspx 代码了

        <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 143px; position: absolute;
            top: 97px" Text="省:" Width="69px"></asp:Label>
        <asp:Label ID="Label2" runat="server" Style="z-index: 101; left: 145px; position: absolute;
            top: 160px" Text="市:" Width="71px"></asp:Label>
        <asp:DropDownList ID="dllPro" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dllPro_SelectedIndexChanged"
            Style="z-index: 102; left: 241px; position: absolute; top: 94px" Width="103px">
        </asp:DropDownList>
        <asp:DropDownList ID="dllCity" runat="server" Style="z-index: 104; left: 242px; position: absolute;
            top: 157px" Width="103px">
        </asp:DropDownList>

下面进入关键,进行编程,很简单哦,代码不多,

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //绑定省
            SqlConnection con = db.Createconnection();
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from province", con);
            SqlDataReader sdr = cmd.ExecuteReader();
            this.dllPro.DataSource = sdr;
            this.dllPro.DataTextField = "proName";
            this.dllPro.DataValueField = "proID";
            this.dllPro.DataBind();
            sdr.Close();
            //绑定市
            SqlCommand cmdcity = new SqlCommand("select * from City where proID=" + this.dllPro.SelectedValue, con);
            SqlDataReader sdr1 = cmdcity.ExecuteReader();
            this.dllCity.DataSource = sdr1;
            this.dllCity.DataTextField = "cityName";
            this.dllCity.DataValueField = "cityID";
            this.dllCity.DataBind();
            sdr.Close();
            //关闭连接
            con.Close();
        }
       
    }

    protected void dllPro_SelectedIndexChanged(object sender, EventArgs e)
    {
        //实现联动效果,选择省后,显示省所属的市
        string proID = this.dllPro.SelectedValue;
        SqlConnection con = db.Createconnection();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from city where proID=" + proID, con);
        SqlDataReader sdr = cmd.ExecuteReader();
        this.dllCity.DataSource = sdr;
        this.dllCity.DataTextField = "cityName";
        this.dllCity.DataValueField = "cityID";
        this.dllCity.DataBind();
        sdr.Close();
    }

9ago.com 专注于.net

posted on 2008-07-28 11:26  9who  阅读(972)  评论(1编辑  收藏  举报

导航