三级联动
2016-07-18 09:49
423 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> .kuang { height:30px; width:100px; } </style> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" CssClass="kuang" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" CssClass="kuang" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList3" runat="server" CssClass="kuang" AutoPostBack="True"></asp:DropDownList> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default4 : System.Web.UI.Page { DataClasses2DataContext context = new DataClasses2DataContext();//linQ方式获取数据库数据 List<ChinaStates> lc = new List<ChinaStates>();//初始化一个Chinastates类的泛型集合 protected void Page_Load(object sender, EventArgs e) { DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//委托方法便捷写法,如果需要查询该方法,把光标移动到要找的对象上,按F12自动跳转 DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged; lc = context.ChinaStates.ToList();//lc接受调取数据库里Chinastates转换为list集合的形式数据 if(IsPostBack==false)//只加载一遍 { //省数据 List<ChinaStates> sheng = lc.Where(r => r.ParentAreaCode == "0001").ToList();//调取省的数据 DropDownList1.DataSource = sheng;//将DropDownList1的数据指向sheng DropDownList1.DataTextField = "AreaName";//DropDownList1的文本文件的名称为AreaName DropDownList1.DataValueField = "AreaCode";//DropDownList1的值得文件名称为AreaCode DropDownList1.DataBind();//DropDownList1绑定数据 //市数据 List<ChinaStates> shi = lc.Where(r => r.ParentAreaCode == DropDownList1.SelectedItem.Value).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); //县数据 List<ChinaStates> xian = lc.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = xian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } } void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { //县数据 List<ChinaStates> xian = lc.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = xian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { //市数据 List<ChinaStates> shi = lc.Where(r => r.ParentAreaCode == DropDownList1.SelectedItem.Value).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); //县数据 List<ChinaStates> xian = lc.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = xian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } }
相关文章推荐
- android快捷开发之Retrofit网络加载框架的简单使用
- OC学习总结之内存管理
- Spring 注入properties文件
- 安装虚拟机增加包--转自linux该这么学
- Yii2中cookie用法示例分析
- 路由器的Loopback地址是什么意思?
- Android,打包成jar,相关知识。20160514
- 数据仓库基础
- EntityFramework、Dapper vs 草根框架性能大比拼,数据库访问哪家强?
- js 中关于立即执行函数的个人理解
- Android酷炫实用的开源框架(UI框架)
- 提交AppStore被拒原因总结
- shell 十三問
- java swing实现页面置换算法模拟系统代码
- myshare Linux2操作篇 -系统日志性能与网络管理
- 超简单将Centos的yum源更换为国内的阿里云源
- Android studio相关技能学习。20160514
- 全面解析Windows下安装 mysql5.7的方法
- JAVA C# C/C++之比教学:继承
- Python字符串的encode与decode研究心得