ASP.net 控件实现数据级联
今天我们来一起用ASP.net实现一个级联,这个小不点应该是会经常用到的的。
咱们简单的画两个窗体。文本框会根据下拉框所选的内容显示不同的内容。
具体实现效果如下
步骤一:
准备工作,建立相应的数据库。
显示效果如下
附脚本如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
create database department use department create table TDepartment ( depID int primary key, depName varchar( 30 ) not null ) insert into TDepartment values( 1 , '教务' ) insert into TDepartment values( 2 , '高校' ) insert into TDepartment values( 3 , '办公室' ) create table emp ( empID int primary key, empName varchar( 30 ) not null , depID int foreign key references TDepartment(depID) ) insert into emp values( 1 , '小马' , 1 ) insert into emp values( 2 , '小丹' , 1 ) insert into emp values( 3 , '小妹' , 1 ) insert into emp values( 4 , '马丹妹' , 3 ) |
步骤二:
新建项目为 ASP.ne Web 窗体应用程序。在窗体中画 图中的两个控件。DropDownList 和ListBox,分别命名为 ddlDep 和lBoxEmp。同时将DropDownList中AutoPostBack属性设置为TRUE(目的是让每次重选列表能有响应)。
我们需要在formload 和ddlDep_SelectedIndexChanged 时间中编写相应的代码。其中ddlDep_SelectedIndexChanged是选中空间ddlDep在右侧属性-事件中双击时间SelectedIndexChanged
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; //数据库 namespace department { public partial class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.ListBox lBoxEmp; protected System.Web.UI.WebControls.DropDownList ddlDep; protected void Page_Load(object sender, EventArgs e) { if (! this .IsPostBack) { SqlConnection con = DBcon.createConnection(); con.Open(); //显示部门 SqlCommand cmd = new SqlCommand( "select * from TDepartment" , con); SqlDataReader sdr = cmd.ExecuteReader(); this .ddlDep.DataSource = sdr; this .ddlDep.DataTextField = "depName" ; this .ddlDep.DataValueField = "depID" ; this .ddlDep.DataBind(); sdr.Close(); //显示员工 SqlCommand cmdEmp = new SqlCommand( "select * from emp where depID=" + this .ddlDep.SelectedValue, con); SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); ; while (sdrEmp.Read()) { this .lBoxEmp.Items.Add( new ListItem(sdrEmp.GetString( 1 ), sdrEmp.GetInt32( 0 ).ToString())); } sdrEmp.Close(); //此处防止用户代码以初始化页面 //关闭连接 con.Close(); } } protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e) { this .lBoxEmp.Items.Clear(); SqlConnection con = DBcon.createConnection(); con.Open(); //显示员工 SqlCommand cmdEmp = new SqlCommand( "select * from emp where depID=" + this .ddlDep.SelectedValue, con); SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); ; while (sdrEmp.Read()) { this .lBoxEmp.Items.Add( new ListItem(sdrEmp.GetString( 1 ), sdrEmp.GetInt32( 0 ).ToString())); } sdrEmp.Close(); //此处防止用户代码以初始化页面 //关闭连接 con.Close(); } } } |
以上是主要内容,下面把获取数据库的语句写在下面。这样便是一个完整的小功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using System; using System.Data.SqlClient; namespace department { public class DBcon { public DBcon(){ } public static SqlConnection createConnection() { SqlConnection con = new SqlConnection( "server=.;database=department;uid=sa;pwd=123456;" ); return con; } } } |
总结:
我们不断的在学习每一个控件的使用,一方面让我们能更加灵活的运用其方法和特性,另一方面也让我们更加熟悉每一种语言。虽然都是一小步一小步的去走,每每成功一个也还是感到喜悦。
ASP.net 控件实现数据级联的更多相关文章
- asp.net控件(1)Repeater
1. 通过Repeater和数据源创建表格 <AlternatingItemTemplate>属性可以控制单元格交替显示不同的背景颜色 <table width=" sty ...
- asp:Repeater控件使用
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式.D ...
- ASP.NET控件属性大全
ASP.NET控件属性大全 DataGridView 控件DataGridView 控件提供用来显示数据的可自定义表.使用 DataGridView 类,可以自定义单元格.行.列和边框. 注意Data ...
- asp.net控件的异步刷新
需求:我们知道,asp.net控件中的button控件,默认是开启了自己主动回发的,而有时候.我们不想刷新整个界面.而仅仅想局部刷新,可页面中又偏偏用到了.net button控件. 尽管我非常讨厌. ...
- asp.net <asp:Content>控件
<asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...
- FineUI 基于 ExtJS 的专业 ASP.NET 控件库
FineUI 基于 ExtJS 的专业 ASP.NET 控件库 http://www.fineui.com/
- 基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串、list集合(MVC5)<二>
上篇博客给大家介绍了基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串.list集合(MVC5)<一>, 其中的两种方式都显得有些冗余.接着上篇博客继续 ...
- C# DataGridView控件清空数据完美解决方法
C# DataGridView控件绑定数据后清空数据在清除DataGridview的数据时: 1.DataSource为NULL(DataGridView.DataSource= null;)这样会将 ...
- ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
随机推荐
- babel的使用(关于使用async报错的问题)
一.配置文件.babelrc .babelrc 文件存放在项目的根目录下. { "presets": [], "plugins": [] } presets 字 ...
- 20181204-2 Final发布
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476 小组介绍 组长:付佳 组员:张俊余 李文涛 孙赛佳 田良 于洋 段 ...
- SCRUM 12.15
今天我们所有的团队成员都加速的进行着开发.虽然最近3门大作业压着,我们还是抽出了足够多的时间对项目的M2阶段进行完善. 今天我们完成了清除缓存的功能,另外我们的单页爬虫也已经设计完成,我们的进度在我们 ...
- week4
History:Commercialization and Growth course Explosive Growth of the Internet and Web The Year of the ...
- Linux命令(二十四) 磁盘管理命令(二) mkfs,mount
一.格式化文件系统 mkfs 当完成硬盘分区以后要进行硬盘的格式化,mkfs系列对应的命令用于将硬盘格式化为指定格式的文件系统.mkfs 本身并不执行建立文件系统的工作,而是去调用相关的程序来执行.例 ...
- [转帖] K8S 常用命令
k8s常用命令 原贴地址 查看集群信息: [root@kubernetes-master pods]# kubectl cluster-info kubectl cluster-info展示结果 k ...
- win7下php7.1运行getenv('REMOTE_ADDR')fastcgi停止运行
// 本地环境phpStudy, PHP7.1.13nts+nginx,切换Apache也不行//ps:php版本<=7.0正常echo "<pre>";// $ ...
- day8——ajax传参到action(Struts2)
第一种:url+?+参数 jsp中: $(function(){ $("[name='delemp']").click(function(){ $this = $(this); $ ...
- SQL 中LTrim、RTrim与Trim的用法
LTrim.RTrim与 Trim 函数 返回 Variant (String),其中包含指定字符串的拷贝,没有前导空白 (LTrim).尾随空白 (RTrim) 或前导和尾随空白 (Trim).语法 ...
- Grafana elasticsearch 应用
早期的时候,项目基于ES+echart写了一些仪表盘的展示页面,虽然ES配合这种char界面有着天然的优势,但实际写起代码来,还是很多重复的劳动,在一次偶然中发现Grafana,看到它提供了很多仪表盘 ...