今天我们来一起用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;
}
}
}
|
总结:
我们不断的在学习每一个控件的使用,一方面让我们能更加灵活的运用其方法和特性,另一方面也让我们更加熟悉每一种语言。虽然都是一小步一小步的去走,每每成功一个也还是感到喜悦。