快速业务通道

ASP.NET 2.0数据教程之三十:格式化DataList和Repeater的数据

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-21
eate a page that uses a DataList to display product information. In the previous tutorial we created a DataList whose ItemTemplate displayed each product’s name, category, supplier, quantity per unit, and price. Let’s repeat this functionality here in this tutorial. To accomplish this, you can either recreate the DataList and its ObjectDataSource from scratch, or you can copy over those controls from the page created in the previous tutorial (Basics.aspx) and paste them into the page for this tutorial (Formatting.aspx).

在学习格式化之前,我们首先创建一 个使用DataList显示product信息的页面.在前面一章里,我们创建了一个 ItemTemplate显示product 的name,category, supplier, quantity和price的 DataList.我们在本章来重复做一次.你可以重新创建DataList和它的 ObjectDataSource,或者直接把前面一章里的Basics.aspx里的控件复制到本章的 页面(Formatting.aspx)里.

当你完成了Formatting.aspx后,将DataList的 ID从DataList1改为ItemDataBoundFormattingExample.

下面,在浏览器里 看看DataList.如图1所示,唯一的格式在于每个product的交替的背景 色.

ASP.NET 2.0数据教程之三十:格式化DataList和Repeater的数据

图 1: 在DataList 里列出product信息

在本章教程里,我们来将 价格小于 $20.00 的product的名字和单价用黄色 高亮来显示.

第二步:在 ItemDataBound Event Handler里编程判断数据的值

由于只有价格低于 $20.00 的product会被格式化,因此我们首先要判断每个product的价格.在绑定数 据到DataList时,DataList 为每条数据源的记录创建一个DataListItem实例,并绑 定数据.当记录绑定到DataListItem对象后,ItemDataBound事件被激发.我们可以 为这个事件创建一个event handler来判断当前DataListItem的值,再根据这个值 来格式化数据.

Create an ItemDataBound event for the DataList and add the following code:

添加以下代码为DataList创建ItemDataBound事 件

C#

protected void ItemDataBoundFormattingExample_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Programmatically reference the ProductsRow instance bound to this DataListItem
Northwind.ProductsRow product =
(Northwind.ProductsRow)((System.Data.DataRowView) e.Item.DataItem).Row;
// See if the UnitPrice is not NULL and less than $20.00
if (!product.IsUnitPriceNull() && product.UnitPrice < 20)
{
// TODO: Highlight the product''s name and price
}
}
}

DataList 的ItemDataBound event handler在概念和语义上来说,和GridView的 RowDataBound event handler一样(见基于数据的自定义格式化),语法上有一点差 别.当ItemDataBound事件激发时,刚刚绑定数据的DataListItem通过e.Item(在 GridView里是e.Row和RowDataBound)传递给相关的event handler.DataList的 ItemDataBound event handler影响到每一行,包括 header , footer 和 separator.但是product信息只绑定到data行.因此,在处理ItemDataBound事件前, 我们首先要判断处理的是否是data行.这个可以通过检查DataListItem的ItemType 属性来完成,它可以有以下八个 值:

AlternatingItem

EditItem

Footer

Header

< p>Item

 

Pager

SelectedItem

Separator

Item和 AlternatingItem都表示DataList的data item.假设我们在处理Item或 AlternatingItem,我们可以获取绑定到当前DataListItem的ProductsRow的实 例.DataListItem的DataItem属性包含了DataRowVi

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号