ASP.NET 三级联动
三级联动就是用三个下拉列表框DropDownList,每个里面添加相应的东西,在第一个列表框中选择一个值,第二三个列表框都会根据第一个选择进行相应的变化,在第二个列表框中选择一个值,第三个列表框也会根据第二个的选择进行变化,通常用于地区选择以及时间日期选择。
注:只有上一个列表框选择执行提交事件,才可以在提交事件中写执行方法,AutoPostBack - 自动提交事件,将DropDownList属性的AutoPostBack改为true
写一个简单的数据库,用三级联动将数据库里的信息查找相应显示
数据库:
写一个省,市,区县的表,市的Pcode为省的Acode,区县的Pcode为市的Acode
create database China
go
use China
go
create table Chinast
(
Acode nvarchar(20),
Aname nvarchar(20),
Pcode nvarchar(20)
) insert into Chinast values('','山东','')
insert into Chinast values('','济南','')
insert into Chinast values('','历下区','')
insert into Chinast values('','天桥区','')
insert into Chinast values('','槐荫区','')
insert into Chinast values('','淄博','')
insert into Chinast values('','张店区','')
insert into Chinast values('','周村区','')
insert into Chinast values('','淄川区','')
insert into Chinast values('','山西','')
insert into Chinast values('','太原','')
insert into Chinast values('','清徐','')
insert into Chinast values('','阳曲','')
insert into Chinast values('','古交','')
insert into Chinast values('','临汾','')
insert into Chinast values('','霍州','')
insert into Chinast values('','洪洞','')
insert into Chinast values('','襄汾','')
写一个Web窗体,里面放三个DropDownList,简单的调整一下
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!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">
.a {
position: relative;
top: 100px;
left: 30%;
height: 40px;
width: 100px;
font-family:微软雅黑;
font-size:25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="a" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="a" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" CssClass="a"></asp:DropDownList>
</form>
</body>
</html>
数据库实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Chinast 的摘要说明
/// </summary>
public class Chinast
{
public Chinast()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public string Acode { get; set; }
public string Aname { get; set; }
public string Pcode { get; set; }
}
数据库操作类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient; /// <summary>
/// ChinastData 的摘要说明
/// </summary>
public class ChinastData
{
SqlConnection conn = null;
SqlCommand cmd = null;
public ChinastData()
{
conn = new SqlConnection("server=.;database=China;user=sa;pwd=123;");
cmd = conn.CreateCommand();
} /// <summary>
/// 根据地区的Pcode查找和此Pcode相应的地区信息
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public List<Chinast> selectAll(string code)
{
List<Chinast> li = new List<Chinast>();
cmd.CommandText = "select * from Chinast where Pcode=@Pcode";
cmd.Parameters.Clear();
cmd.Parameters.Add("@Pcode", code);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Chinast st=new Chinast();
st.Acode=dr["Acode"].ToString();
st.Aname= dr["Aname"].ToString();
st.Pcode=dr["Pcode"].ToString();
li.Add(st);
}
}
conn.Close();
return li;
} }
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//省提交事件委托
DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;//市提交事件委托
if (IsPostBack == false)//判断IsPostBack为false,将代码写在里面,页面只执行一次,防止每次操作进行页面刷新
{
//调用数据库方法,返回一个Chinast泛型集合,传参省的Pcode
List<Chinast> sheng = new ChinastData().selectAll("");
DropDownList1.DataSource = sheng;//绑定数据源
DropDownList1.DataTextField = "Aname";//下拉框里面显示的值,DataTextField为返回集合中的Aname,地名
DropDownList1.DataValueField = "Acode";//Value为他的Acode;
DropDownList1.DataBind(); //因为每个市的Pcode为省的Acode,Acode为省的Value值,则根据市的Pcode查数据时就是省的Value,DropDownList1.SelectedItem.Value
List<Chinast> shi = new ChinastData().selectAll(DropDownList1.SelectedItem.Value);
DropDownList2.DataSource = shi;
DropDownList2.DataTextField = "Aname";
DropDownList2.DataValueField = "Acode";
DropDownList2.DataBind(); //因为每个区县的Pcode为市的Acode,Acode为市的Value值,则根据区县的Pcode查数据时就是市的Value,DropDownList2.SelectedItem.Value
List<Chinast> qu = new ChinastData().selectAll(DropDownList2.SelectedItem.Value);
DropDownList3.DataSource = qu;
DropDownList3.DataTextField = "Aname";
DropDownList3.DataValueField = "Acode";
DropDownList3.DataBind(); } } void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)//市提交事件
{
List<Chinast> qu = new ChinastData().selectAll(DropDownList2.SelectedItem.Value);//根据选择市的Value查找相应的区县
DropDownList3.DataSource = qu;
DropDownList3.DataTextField = "Aname";
DropDownList3.DataValueField = "Acode";
DropDownList3.DataBind();
} void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//省提交事件,选择后执行提交,市及区县根据Value值执行相应的查找显示
{
List<Chinast> shi = new ChinastData().selectAll(DropDownList1.SelectedItem.Value);//根据选择省的Value查找相应的市
DropDownList2.DataSource = shi;
DropDownList2.DataTextField = "Aname";
DropDownList2.DataValueField = "Acode";
DropDownList2.DataBind(); List<Chinast> qu = new ChinastData().selectAll(DropDownList2.SelectedItem.Value);//根据选择市的Value查找相应的区县
DropDownList3.DataSource = qu;
DropDownList3.DataTextField = "Aname";
DropDownList3.DataValueField = "Acode";
DropDownList3.DataBind();
} }




ASP.NET 三级联动的更多相关文章
- 2014.12.06 ASP.NET 三级联动,添加员工,修改员工
(一)三级联动 要实现的效果: 代码: MyDBDataContext context = new MyDBDataContext(); protected void Page_Load(object ...
- asp.net_MVC_jq三级联动
数据库结构 建立三张表,Association,Team,Player 关系如下: 建立asp.net MVC 3项目,在HomeController.cs中利用Linq to SQL获取数据 首先实 ...
- ASP.NET实现省市区三级联动(局部刷新)
跟前一篇ASP.NET实现年月日三级联动(局部刷新)一样,没什么技术含量,直接上代码 <asp:ScriptManager ID="ScriptManager1" runat ...
- 在ASP.NET MVC中实现一种不同于平常的三级联动、级联方式, 可用于城市、车型选择等多层级联场景
三级或多级联动的场景经常会碰到,比如省.市.区,比如品牌.车系.车型,比如类别的多级联动......我们首先想到的是用三个select来展示,这是最通常的做法.但在另外一些场景中,比如确定搜索条件的时 ...
- webForm(三)——三级联动
三级联动 首先附图一张,初步认识一下什么是三级联动: 注:选第一个后面两个变,选第二个,最后一个改变. 其次,做三级联动需要注意的方面:①DropD ...
- 用DropDownList实现的省市级三级联动
这是一个用DropDownList 实现的省市级三级联动,记录一下········ <asp:ScriptManager ID="ScriptManager1" runat= ...
- ajax验证表单元素规范正确与否 ajax展示加载数据库数据 ajax三级联动
一.ajax验证表单元素规范正确与否 以用ajax来验证用户名是否被占用为例 1创建表单元素<input type="text" id="t"> 2 ...
- 20150303--从SQL中获取数据的三级联动
省市地区的三级联动,每变更一次所选地都需要提交,但是又不需要把整个页面提交,所以我们需要使用控件:UdataPanel.工具--AJAX扩展 还有ScriptManager,并要将其放在页面的最顶端. ...
- Web 1三级联动 下拉框 2添加修改删除 弹框
Web 三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
随机推荐
- Windows向虚拟机Linux传输文件方法
在Windows中装了个centOS,进行文件操作时,把mv写成了rm,然后就悲剧了.. 赶紧从网上找来文件的具体内容,然后由Windows向Linux挂载共享文件夹. 具体做法: 在Windows中 ...
- oracle提高查询效率的34个方面全解析
oracle提高查询效率的34个方面全解析 在一个数据库中进行操作的时候,效率是很重要的,那么,如何提高oracle的查询效率呢?笔者将从以下几个方面进行详细解析: 1.选择最有效率的表名顺序(只 ...
- Python strip lstrip rstrip使用方法(字符串处理空格)
Python strip lstrip rstrip使用方法(字符串处理空格) strip是trim掉字符串两边的空格.lstrip, trim掉左边的空格rstrip, trim掉右边的空格 s ...
- 虚拟机中安装CentOS7
初始安装: 1.安装新虚拟机时,选择稍后安装操作系统,这可以自己设置语言等信息 2.修改自定义硬件:为网卡生成一个mac地址,(这里需要注意,有时网卡会冲突,导致连接时好时坏,以后可以删除掉网卡,重新 ...
- SAP HANA HDBSQL命令
1.登录: window操作系统下面: 打开C:\Program Files\SAP\hdbclient\hdbsql.exe \c 连接数据库 connect的缩写 HANA hostname: ...
- List转数组
eg: List<Product> products = new ArrayList<Product>(); Product[] array = products.toArra ...
- 【ASP.NET 进阶】判断访问网站的客户端是PC还是手机
主要就是通过客户端传递的User-agent来判断访问网站的客户端是PC还是手机,.NET中就是Request.ServerVariables["HTTP_USER_AGENT"] ...
- SqlDataSource.FilterExpression Property
SqlDataSource.FilterExpression Property 定义 命名空间: System.Web.UI.WebControls Assembly: System.Web.dll ...
- VC 字符串转化和分割
原文:点击这里. 备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯 1.格式化字符串 CString s;s.Format(_T("The num is %d."), ...
- linux crontab 保证php守护进程运行
写好php脚本.建议定期检测内存占用,核心逻辑就不写了.这个跟业务有关. if(memory_get_usage()>100*1024*1024){ exit(0);//大于100M内存退出程序 ...