Book Excerpt: Working with the DataList Control
In this chapter, we will cover the ASP.NET DataList control. We will learn about
the following:
|
|
This chapter excerpt from
ASP.NET Data Presentation Controls Essentials by Joydip Kanjilal,
is printed with permission from Packt Publishing
, Copyright 2007.
Handling Events
The Repeater, DataList, and DataGrid controls support event bubbling. What is
event bubbling? Event Bubbling refers to the ability of a control to capture
the events in a child control and bubble up the event to the container whenever
an event occurs. The DataList control supports the following six events:
ItemCreated
ItemCommand
EditCommand
UpdateCommand
DeleteCommand
CancelCommand
We will now discuss how we can work with the events of the DataList control. In
order to handle events when working with a DataList control, include a Button
or a LinkButton control in the DataList control. These controls have click
events that can be used to bubble up the triggered event to the container
control, that is, the DataList.
The following code snippet illustrates how you can attach a handler to an
ItemCommand event of a DataList control:
<asp:DataList ID="DataList1" runat="server" onItemCommand =
"ItemCommandEventHandler"/>
The corresponding handler that gets called whenever the event is fi red is as
follows:
void ItemCommandEventHandler (Object src, DataListCommandEventArgs e ....)
{
//Some event handling code
}
Simi larly, you can handle the ItemCreated event by specifying the handler in
the .aspx file as follows:
<asp:DataList ID="DataList1" runat="server" onItemCreated =
"ItemCreatedEventHandler" />
The corresponding handler that is triggered whenever this event occurs is as
follows.
void ItemCreatedEventHandler ( Object src, DataListCommandEventArgs e )
{
//Some event handling
code
}
Similarly, you can use the CancelCommand event by specifying the event handler
in your .aspx file as follows:
<asp:DataList ID="DataList1" runat="server" onCancelCommand =
.........."CancelCommandEventHandler" />
The corresponding handler that is triggered whenever this event occurs is as
follows.
void ItemCreatedEventHandler ( Object src, DataListCommandEventArgs e )
{
//Some event handling code
}
Similarly, you can use the CancelCommand event by specifying the event handler
in your .aspx file as follows:
<asp:DataList ID="DataList1" runat="server" onCancelCommand =
.........."CancelCommandEventHandler" />
The corresponding event handler that would get fi red is as follows:
void CancelCommandEventHandler ( Object src, DataListCommandEventArgs e )
{
//Some event handling code
}
You can handle any of the other events similarly and execute your event handlers
appropriately.
|