web analytics

How a button works in an <asp:DataList> control?

Options

codeling 1605 - 6678
@2026-07-02 16:55:35

In ASP.NET Web Forms, the CommandName property of a button placed inside an <asp:DataList> control is used to identify the action to be performed when that specific button is clicked

Because a DataList repeats its templates for every row in your data source, you cannot easily create a standard, individual Click event handler for every generated button. Instead, ASP.NET uses a mechanism called event bubbling. When any button inside the DataList is clicked, it bubbles the event up to the parent DataList, which fires a single ItemCommand event

The CommandName acts as a label telling the ItemCommand event handler exactly which type of action was triggered.

@2026-07-02 16:56:51

Types of Command Names

You can assign any string value to the CommandName property, split into two main categories: 

1. Predefined (Reserved) Commands

The DataList control inherently recognizes five specific command names. When you use these, the ItemCommand event fires, but the DataList also raises a dedicated, built-in event wrapper for convenience:

  • "Edit": Triggers the EditCommand event. Typically used to switch a row into editable mode.
  • "Cancel": Triggers the CancelCommand event. Used to abandon edits and return the row to read-only mode.
  • "Update": Triggers the UpdateCommand event. Used to save modified row data back to a database.
  • "Delete": Triggers the DeleteCommand event. Used to remove a record from the underlying data.
  • "Select": Triggers the SelectedIndexChanged event. Used to highlight or select a specific row.

2. Custom Commands

You can invent any custom string (e.g., "AddToCart", "ViewDetails", "Like") to handle unique logic. These custom actions are caught entirely inside the ItemCommand event handler using conditional logic.

@2026-07-02 16:58:40

Example

1. The ASPX Markup

In this example, we have two buttons in the ItemTemplate. One utilizes a predefined command ("Edit"), and the other uses a custom command ("AddToCart"). We also use CommandArgument to pass the unique ID of the record so the server code knows which row to affect.

html

<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
    <ItemTemplate>
        <!-- Display bound data -->
        Name: <%# Eval("ProductName") %> <br />

        <!-- Predefined command button -->
        <asp:Button ID="btnEdit" runat="server" 
                    Text="Edit Item" 
                    CommandName="Edit" />

        <!-- Custom command button -->
        <asp:Button ID="btnCart" runat="server" 
                    Text="Add to Cart" 
                    CommandName="AddToCart" 
                    CommandArgument='<%# Eval("ProductID") %>' />
    </ItemTemplate>
</asp:DataList>

Use code with caution.

2. The Code-Behind (C#)

In your server-side code, you write a single method to handle the OnItemCommand event. The DataListCommandEventArgs e object supplies the CommandName and CommandArgument from the clicked button. 

csharp

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    // Check which command name was sent
    if (e.CommandName == "Edit")
    {
        // Put the clicked row into edit mode
        DataList1.EditItemIndex = e.Item.ItemIndex;
        BindData(); // Rebind your data source to update the view
    }
    else if (e.CommandName == "AddToCart")
    {
        // Extract the unique ID passed from the CommandArgument
        string productId = e.CommandArgument.ToString();
        
        // Execute your custom logic
        AddProductToCart(productId);
    }
}

Key Takeaways
  • Centralization: CommandName funnels your user interactions into a single block of server-side logic (ItemCommand), rather than managing individual button pointers.
  • Pairs with CommandArgument: While CommandName tells the server what action to take, the CommandArgument property tells the server which record to take that action on. 

Comments

You must Sign In to comment on this topic.


© 2026 Digcode.com