呵呵,其实这个好好想想的话,很简单的,归根到底需要用一个事件SelectedIndexChanged!呵呵,下面我就给你演示哈
--用来实现三级联动的表
create table AddressInfo
(
cityId int,
cityName varchar(20),
orderId int
)
go
insert into AddressInfo values(1,'重庆',0)
insert into AddressInfo values(2,'上海',0)
insert into AddressInfo values(3,'安徽',0)
insert into AddressInfo values(4,'沙坪坝',1)
insert into AddressInfo values(5,'渝北区',1)
insert into AddressInfo values(6,'巴南区',1)
insert into AddressInfo values(7,'杨浦区',2)
insert into AddressInfo values(8,'南汇区',2)
insert into AddressInfo values(9,'普陀区',2)
insert into AddressInfo values(10,'合肥市',3)
insert into AddressInfo values(11,'安庆市',3)
insert into AddressInfo values(12,'芜湖市',3)
insert into AddressInfo values(13,'上桥',4)
insert into AddressInfo values(14,'新桥',4)
insert into AddressInfo values(15,'龙兴',5)
insert into AddressInfo values(16,'石船',5)
insert into AddressInfo values(17,'巴南1',6)
insert into AddressInfo values(18,'巴南2',6)
insert into AddressInfo values(19,'杨浦区1',7)
insert into AddressInfo values(20,'杨浦区2',7)
insert into AddressInfo values(21,'南汇区1',8)
insert into AddressInfo values(22,'南汇区2',8)
insert into AddressInfo values(23,'普陀区1',9)
insert into AddressInfo values(24,'普陀区2',9)
insert into AddressInfo values(25,'肥西',10)
insert into AddressInfo values(26,'肥东',10)
insert into AddressInfo values(27,'枞阳县',11)
insert into AddressInfo values(28,'望江县',11)
insert into AddressInfo values(29,'芜湖1',12)
insert into AddressInfo values(30,'芜湖2',12)
go
//实现步骤
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
Content city = new Content();
DataTable dt = null;
//在加载事件里初始化省
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strSql = string.Format("select * from addressInfo where orderId={0}", 0);
dt = city.Select(strSql);
this.ddlCity.DataSource = dt;
this.ddlCity.DataTextField = "cityName";
this.ddlCity.DataValueField = "cityId";
this.ddlCity.DataBind();
ddlCity_SelectedIndexChanged(sender, e);
}
}
//SelectedIndexChanged事件,当选择省的时候,该干什么(这里解释当你选择省的时候,显示该省所对应的市)
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlCity.SelectedValue!=null)
{
string strSql = string.Format("select * from addressInfo where orderId={0}", this.ddlCity.SelectedValue);
dt = city.Select(strSql);
this.ddlQu.DataSource = dt;
this.ddlQu.DataTextField = "cityName";
this.ddlQu.DataValueField = "cityId";
this.ddlQu.DataBind();
ddlQu_SelectedIndexChanged(sender, e);
}
}
protected void ddlQu_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlQu.SelectedValue != null)
{
string strSql = string.Format("select * from addressInfo where orderId={0}", this.ddlQu.SelectedValue);
dt = city.Select(strSql);
this.ddlXian.DataSource = dt;
this.ddlXian.DataTextField = "cityName";
this.ddlXian.DataValueField = "cityId";
this.ddlXian.DataBind();
}
}
}