熟悉DropDownList和ObjectDataSource的配合使用

开发 后端
本文介绍了一个ASP.NET DropDownList范例,用于显示选定类别的对象。该功能和DropDownList的正常用法一样,通过DropDownList和ObjectDataSource的配合使用而实现。

Categories DropDownList完成后, 下一步就需要一个显示属于选定类别的产品的DropDownList了. 要完成这个功能, 再增加一个DropDownList控件并命名为ProductsByCategory. 与Categories DropDownList一样, 为ProductsByCategory DropDownList创建一个新的ObjectDataSource并命名为ProductsByCategoryDataSource.

为ProductsByCategory DropDownList添加新数据源 

DropDownList和ObjectDataSource: 为ProductsByCategory DropDownList添加新数据源

创建一个新的 ObjectDataSource 并命名为ProductsByCategoryDataSource 

DropDownList和ObjectDataSource: 创建一个新的 ObjectDataSource 并命名为ProductsByCategoryDataSource

由于ProductsByCategory DropDownList 需要显示属于选定类别的产品, 让ObjectDataSource 调用ProductsBLL对象的GetProductsByCategoryID(categoryID)方法.

选择使用ProductsBLL类 

DropDownList和ObjectDataSource: 选择使用ProductsBLL类

配置ObjectDataSource 使用GetProductsByCategoryID(categoryID)方法 

DropDownList和ObjectDataSource: 配置ObjectDataSource 使用GetProductsByCategoryID(categoryID)方法

在向导的***一步需要指定categoryID的值. 我们把Categories DropDownList的已选择的数据项项作为该参数值.

使用categoryID参数从Categories DropDownList接收值 

DropDownList和ObjectDataSource: 使用categoryID参数从Categories DropDownList接收值

ObjectDataSource配置完成后, 剩下的就是指定DropDownList要显示的字段和作为值的字段了. 设置为显示ProductName并把ProductID做为值字段

指定 DropDownList数据项的文本和值使用的数据源字段 

DropDownList和ObjectDataSource: 指定 DropDownList数据项的文本和值使用的数据源字段

ObjectDataSource 和ProductsByCategory DropDownList配置完成后页面上会有两个DropDownList: ***个列出所有的类别,第二个列出属于选定类别的产品. 当用户在***个DropDownList上选择了一个新的类别后, 将会发生一次回发(postback),第二个DropDownList将会重新绑定以显示属于新选定类别的产品. 图12 和图13显示了在浏览器中看到的MasterDetailsDetails.aspx页面.

***次访问页面时Beverages 类别是选中的. 

DropDownList和ObjectDataSource: ***次访问页面时Beverages 类别是选中的.

选择一个不同的类别时显示该类别的产品 

DropDownList和ObjectDataSource: 选择一个不同的类别时显示该类别的产品

激活productsByCategory DropDownList的 AutoPostBack属性  

DropDownList和ObjectDataSource: 激活productsByCategory DropDownList的 AutoPostBack属性

【编辑推荐】

  1. ASP.NET 2.0数据教程:给站点添加aspx页面
  2. ASP.NET 2.0数据教程:创建母版页
  3. ASP.NET跨页面传值的技巧
  4. ASP.NET 2.0数据教程:给DAL添加定制编码
  5. ASP.NET 2.0数据教程:完成数据访问层
责任编辑:book05 来源: 博客堂
相关推荐

2009-07-27 10:12:37

GridView和Ob

2009-07-27 09:45:14

ASP.NET Dro

2009-10-29 09:15:32

ASP.NET MVCDropDownLis

2020-09-23 10:59:00

开发技能代码

2009-07-22 11:11:39

iBATIS分页实例ObjectDataS

2009-07-27 08:51:24

ObjectDataS

2009-12-08 10:50:36

Windows 7使用

2020-07-11 09:25:15

Python编程语言代码

2011-04-29 12:54:05

笔记本屏幕

2024-04-28 00:00:00

密码密钥熟悉度

2009-11-17 10:31:12

Solaris项目管理任务管理

2009-09-03 09:44:02

DropDownLisC#递归

2009-07-27 09:01:44

ObjectDataS

2009-07-27 10:22:43

ASP.NET Dro

2010-08-12 09:35:17

华为路由器网守

2010-08-09 13:21:49

路由器和网守

2010-05-25 16:11:25

Git-SVN

2022-04-24 09:54:24

ProxyReflect前端

2021-06-09 11:28:06

加密数据Jsencrypt

2020-11-30 06:56:16

C++Explicit关键词
点赞
收藏

51CTO技术栈公众号