您的位置:首页 > 其它

三级联动

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

}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: