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 departmentuse departmentcreate 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是一样 ...
随机推荐
- 手机Gmail上用Exchange协议配置收发QQ邮箱
1.开启Exchange服务 2.生成授权码(登录密码) 3."服务器"填入ex.qq.com
- AssetBundleMaster
AssetBundleMaster is an integrated solution for build AssetBundle and load assets from AssetBundles ...
- 504. Base 7
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- linuxC/C++面试问题总结整理
linuxC/C++面试问题总结整理 因为一些原因重新找工作了,面的linux c/c++,这里把面试中经常碰到的问题总结一下. linuxC/C++面试问题总结整理 单元测试 关键字const 关键 ...
- 金蝶盘点机PDA仓库条码管理家电类序列号扫描操作方法-采购入库单
1.1. 采购入库单 传统的进销存管理软件需要人工识别商品品种,清点商品数量,然后再去人工手工在电脑上一行行的录入采购入库单.录单效率低,误差大. 如果使用汉码盘点机PDA,入库时,仓管员只需要手持 ...
- Daily Scrum - 12/02
Meeting Minutes Shuo终于把文件存取弄好了!Wei大致把后端的代码整合好了,现在是可以基本实现算法的一个简易背单词版本.另外我们又交流了一下UI方面的事情,发现还是有一些问题(特别是 ...
- VS2013安装及测试练习
一.安装过程 任务:安装VS2010以上的版本. 其实很闹心,因为看了一下VS的安装包,都很大.以学校的网速,得下到什么时候?这是第一想法. 挺麻烦,也挺周折,最终下好了安装包.但是,还是出了问题,在 ...
- Oracle与SQLSERVER修改数据文件的路径
1. SQLSERVER ALTER DATABASE CWBASEMSS modify file (name = cwbasemss_dat ,filename = 'c:\cwdata\mss\C ...
- [转帖]shell 中的>/dev/null 2>&1 是什么鬼?
shell 中的>/dev/null 2>&1 是什么鬼? http://blog.jobbole.com/109355/ 背景 我们经常能在shell脚本中发现>/dev/ ...
- NGINX.conf配置文件支持pathinfo
# power by www.php.cn #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/e ...