<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
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 lc = new List ();//初始化一个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 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 shi = lc.Where(r => r.ParentAreaCode == DropDownList1.SelectedItem.Value).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); //县数据 List 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 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 shi = lc.Where(r => r.ParentAreaCode == DropDownList1.SelectedItem.Value).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); //县数据 List xian = lc.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = xian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); }}