asp.net-省市县三级联动

 事件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)


--------代码--------------------------------

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();
        }
    }
 }

注:

这是在百度看到的一个回答,思路很清晰,简洁,原理就是利用AutoPostBack属性,点击绑定对应数据。缺点就是刷新,来回到数据库查询,绑定数据

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值