The following example shows how you can respond to a button click in a DataList control. In the example, the ItemTemplate contains an ImageButton control that displays a shopping cart. The button sends the command AddToCart. The ItemCommand event handler determines which button was clicked, and — if it was the shopping cart button — performs the appropriate logic.
Protected Sub DataList1_ItemCommand(ByVal source As Object, _
ByVal e As DataListCommandEventArgs)
If e.CommandName = "AddToCart" Then
' Add code here to add the item to the shopping cart.
' Use the value of e.Item.ItemIndex to retrieve the data
' item in the control.
End If
End Sub
protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Add code here to add the item to the shopping cart.
// Use the value of e.Item.ItemIndex to retrieve the data
// item in the control.
}
}