下面就是在提交按钮的单击事件中填写代码(代码区)(前提是把省市县的数据库建好)

protected void Page_Load(object sender, EventArgs e)         {             if (!this.Page.IsPostBack)             {                 getddlProvinceDataBind();       //页面首次加载执行省份绑定

}         }            public void getddlProvinceDataBind()       //省份数据绑定         {             string sqlProvince = "SELECT * FROM province";             DropDownList2.DataSource = getDataSet(sqlProvince);             DropDownList2.DataTextField = "province";             DropDownList2.DataValueField = "provinceID";             DropDownList2.DataBind();

DropDownList2.Items.Insert(0, new ListItem("--省份--", "0"));         }         protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)         {             //第一层,省份选择事件             {                 int ProvinceID = Convert.ToInt32(DropDownList2.SelectedValue);                 if (ProvinceID > 0)                 {                     string sqlCity = "SELECT * FROM city WHERE father=" + ProvinceID + "";       //根据省份ID找城市                     DropDownList3.DataSource = getDataSet(sqlCity);                     DropDownList3.DataTextField = "city";                     DropDownList3.DataValueField = "cityID";                     DropDownList3.DataBind();

DropDownList3.Items.Insert(0, new ListItem("--请选择城市--", "0"));                 }                 else                 {                     DropDownList3.Items.Clear();                     DropDownList3.Items.Insert(0, new ListItem("--请选择城市--", "0"));                     DropDownList3.Items.Clear();                     DropDownList3.Items.Insert(0, new ListItem("--请选择县区--", "0"));                 }             }         }

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)         {             //第二层,城市件             {                 int CityID = Convert.ToInt32(DropDownList3.SelectedValue);                 if (CityID > 0)                 {                     string sqlDistrict = "SELECT * FROM area  WHERE father=" + CityID + "";       //根据城市ID找县区                     DropDownList4.DataSource = getDataSet(sqlDistrict);                     DropDownList4.DataTextField = "area";                     DropDownList4.DataValueField = "areaID";                     DropDownList4.DataBind();

DropDownList4.Items.Insert(0, new ListItem("--请选择县区--", "0"));                 }                 else                 {                     DropDownList4.Items.Clear();                     DropDownList4.Items.Insert(0, new ListItem("--请选择县区--", "0"));                 }             }         }

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)         {             int ProvinceID = Convert.ToInt32(DropDownList2.SelectedValue);             int CityID = Convert.ToInt32(DropDownList3.SelectedValue);             int DistrictID = Convert.ToInt32(DropDownList4.SelectedValue);             //if (ProvinceID > 0 && CityID > 0 && DistrictID > 0)             //{             //    Response.Write("您选择的省份ID:" + ProvinceID + "城市ID:" + CityID + "县区ID:" + DistrictID + "");             //}         }         public DataSet getDataSet(string sql)       //自定义方法,sql语句参数,返回DataSet数据集         {             string connection = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;             SqlConnection conn = new SqlConnection(connection);             SqlDataAdapter sda = new SqlDataAdapter(sql, conn);             DataSet ds = new DataSet();             sda.Fill(ds);             return ds;         }

这样,就完成我们想要的效果了。如下图所示:

最新文章

  1. PHP Mongodb 基本操作
  2. SilkTest天龙八部系列4-ChildWin
  3. MINA源码阅读之Future系
  4. HTML DOM节点
  5. windows下php+apache+mysql环境搭建
  6. 命名空间“System.Windows.Forms”中不存在类型或命名空间名称“DataVisualization”。是否缺少程序集引用?
  7. [0] Lc.exe 已退出,代码 -1
  8. vue学习前奏——webpack
  9. [UOJ UNR#1]奇怪的线段树
  10. 如何解决夜神模拟器连不上adb的问题
  11. vue初始化页面dom操纵 $nextTick
  12. shell 三剑客
  13. php安全
  14. [LOJ#6033]. 「雅礼集训 2017 Day2」棋盘游戏[二分图博弈、匈牙利算法]
  15. leetcode217
  16. 支付宝hr终面,忐忑的等待结果
  17. leetcode 无重复字符的最长子串 python实现
  18. 优秀的PHP开发者是怎样炼成的?
  19. Hive 外部表新增字段或者修改字段类型等不生效
  20. (转)python标准库中socket模块详解

热门文章

  1. CF469D Two Set (并查集)
  2. PHP中逻辑运算符and/or与||/&&的一个坑
  3. QT中Sqlite的使用
  4. linux下gedit读取txt乱码解决办法
  5. android版微信5.2.1更新 支持微信聊天记录备份到电脑上
  6. 【C语言入门教程】2.1 数据类型(5种基本数据类型),聚合类型与修饰符
  7. webrtc第一篇
  8. ACdream1063——平衡树
  9. native2ascii.exe
  10. OpenCV成长之路(7):图像滤波