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是一样 ...
随机推荐
- linux一切皆文件之tty字符设备(深入理解sshd创建pty的过程) (五)
一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.块设备支 ...
- ElasticSearch读写原理
es 写入数据的工作原理是什么啊?es 查询数据的工作原理是什么啊?底层的 lucene 介绍一下呗?倒排索引了解吗? es 写数据过程 客户端选择一个 node 发送请求过去,这个 node 就是 ...
- Redis学习笔记之底层数据结构
1.简单动态字符串(simple dynamic string, SDS) 定义: struct sdshdr { int len;//记录buf中使用的字节数量 int ...
- 软件工程 BUAAMOOC项目Postmortem结果
设想和目标 1.我们的软件要解决什么问题?是否定义的很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件是基于北航MOOC网站做的Android手机客户端,用于便捷的在学校里通过手机做到随时随地 ...
- C语言版本:单链表的实现
slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #inc ...
- 关于github的使用心得
https://github.com/JavaLizheng/test git常用命令: git config :配置git git add:更新working directory中的文件至stagi ...
- 第一个spring,第一天。
陈志棚:界面跳转与框架 李天麟:游戏界面ui 徐侃:算法代码的设计 经过热烈的讨论后,我们各自在完成自己的任务.
- iOS之Block总结以及内存管理
block定义 struct Block_descriptor { unsigned long int reserved; unsigned long int size; void (*copy)(v ...
- ElasticSearch 2 (32) - 信息聚合系列之范围限定
ElasticSearch 2 (32) - 信息聚合系列之范围限定 摘要 到目前为止我们看到的所有聚合的例子都省略了搜索请求,完整的请求就是聚合本身. 聚合与搜索请求同时执行,但是我们需要理解一个新 ...
- DOM父节点、子节点例子
父节点 <body> <ul id="oUl"> <li><a href="#">隐藏1</a>&l ...