王朝网络
分享
 
 
 

ASP.NET Unleashed--Using the DataList and DataGrid Controls(1)

王朝asp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

Overview of the DataList and DataGrid Controls

The DataList and DataGrid controls share many of the same features. Before getting into the details of how each of these controls work, you will learn about some of the common properties of the controls. In the following sections, you will learn how both controls support event bubbling, templates, and the DataKeys collection.

Understanding Event Bubbling

Three of the standard controls included with the ASP.NET framework support event bubbling: the Repeater, DataList, and DataGrid controls. These controls enable you to capture events in their child controls. When an event is raised in a child control, the event "bubbles up" to the containing control, and the containing control can then execute a subroutine to handle the event.

As you saw in the preceding chapter, for example, you can display a list of LinkButton controls in a Repeater control. When you click a LinkButton control, it raises a Click event. Instead of writing code to handle the Click event for each LinkButton individually, you can associate one subroutine with any Click event that is raised in the Repeater control.

The Repeater, DataList, and DataGrid controls enable you to associate a subroutine with an event bubbled up from a child control by specifying a value for the OnItemCommand property. For example, in the case of the Repeater control, you would associate a subroutine with the ItemCommand event like this:

<asp:Repeater

OnItemCommand="Repeater_ItemCommand"

Runat="Server">

<ItemTemplate>

<li>

<asp:LinkButton

Text='<%# Container.DataItem( "CategoryName" ) %>'

Runat="Server" />

</ItemTemplate>

</asp:Repeater>

When any of the LinkButton controls rendered by the Repeater control are clicked and the Click event occurs, the Repeater_ItemCommand subroutine executes to handle the event.

The DataList and DataGrid controls enable you to go one step further. Both of these controls enable you to make distinctions between the events raised by their child controls.

The DataList and DataGrid controls support the following five events:

ItemCommand

UpdateCommand

EditCommand

DeleteCommand

CancelCommand

When you create the child controls in a DataList or DataGrid, you can indicate the type of event to raise in the containing control by using the CommandName property. For example, if you want to raise an UpdateCommand event, you would specify the CommandName property like this:

<asp:LinkButton

Text="Update Field!"

CommandName="update"

Runat="Server" />

Three ASP.NET controls have a CommandName property: LinkButton, Button, and ImageButton. All three controls also have a CommandArgument property that enables you to specify an optional argument that is passed when the control is clicked. So, you can bubble up an optional argument by using an event like this:

<asp:LinkButton

Text="Update Field!"

CommandName="update"

CommandArgument="12"

Runat="Server" />

Using Templates

Templates were introduced in the preceding chapter. The Repeater control supports several templates that enable you to format the output of the control. For example, you can use the ItemTemplate to format how the Repeater control renders each item that it displays.

The DataList and DataGrid controls also support templates. The DataList control supports all the same templates as the Repeater control, in addition to two others:

HeaderTemplate— Controls how the header of the DataList is formatted

ItemTemplate— Controls the formatting of each item displayed by the DataList

AlternatingItemTemplate— Controls how alternate items are formatted

SeparatorTemplate— Displays a separator between each item displayed by the DataList

FooterTemplate— Controls how the footer of the DataList is formatted

SelectedItemTemplate— Controls how a selected item is formatted

EditItemTemplate— Controls how an item selected for editing is formatted

When you select an item in a DataList, the SelectedItem template is displayed. When you select an item for editing in a DataList, the EditItem template is displayed.

The DataGrid control supports the following four templates when you display a column using a TemplateColumn:

HeaderTemplate— Controls how the header of the DataGrid is formatted

ItemTemplate— Controls the formatting of items displayed by the DataGrid

FooterTemplate— Controls how the footer of the DataGrid is formatted

EditItemTemplate— Controls how an item selected for editing is formatted

The EditItemTemplate is displayed when you select an item for editing in a DataGrid.

When working with the DataList and DataGrid controls, you often run into situations in which you need to retrieve a particular control contained in a template. You can find a particular control in a template by using the FindControl() method.

You can use the FindControl() method, for example, with the Controls collection of a DataListItem to find a particular control in a DataList template. You can use the FindControl() method with the Controls collection of a DataGridItem to find a particular control in a DataGrid template. I will discuss some concrete examples of how to use the FindControl() method later in this chapter.

Using the DataKeys Collection

Both the DataList and DataGrid controls have a special collection named DataKeys. This collection represents the primary key field associated with each item in the control.

Imagine, for example, that you have a database table named Products that you want to display and edit with a DataGrid control. Imagine, furthermore, that the Products table has an identity column named ProductID.

You can associate the value of the identity column with each item in the DataGrid by assigning the name of the identity column to the DataKeyField property of the DataGrid like this:

<asp:DataGrid

DataKeyField="ProductID"

Runat="Server" />

The advantage of associating the identity column with each item in the DataGrid is that you can retrieve the value of this column when you edit a field in the DataGrid. If you update a field in the DataGrid and want to update the corresponding field in the underlying database table, you can use the DataKeys collection to uniquely identify the correct row in the database table to update.

You'll come across several situations in which the DataKeys collection will prove valuable later in this chapter.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有