Overview of the DataList and DataGrid Controls


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.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net