Handle the button click event from an ASP.NET ListView control

Here is how you can handle the event of a button from within an ASP.NET ListView control.

Add your button to the ListView Template like you would any other control:

<asp:ListView ItemPlaceholderID="Test" runat="server" ID="ListView1" >
        <asp:Button runat="server" Text='<%# Eval("MyProperty") %>' CommandArgument='<%# Eval("MyProperty") %>' /><br />


And then handle the ListView.ItemCommand event in your code:


That is really all there is to it.  The ListView will bubble up the Button’s click event to the ListView_ItemCommand event handler and then you can handle it there.  You can also use the CommandArgument property to send button specific data, such as the particular ID of the row or item that the user clicked the button for.

This call also be used on some of the other databound controls such as the ASP.NET Repeater.


Full Example Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListViewButtonExample.aspx.cs" Inherits="ListViewButtonExample" %>
    <form id="form1" runat="server">
        <asp:ListView ItemPlaceholderID="Test" runat="server" ID="ListView1" >
                <asp:Literal runat="server" ID="Test"></asp:Literal>
                <asp:Literal runat="server" Text='<%# Eval("MyProperty") %>'></asp:Literal>
                <asp:Button runat="server" Text='<%# Eval("MyProperty") %>' CommandArgument='<%# Eval("MyProperty") %>' /><br />
        <asp:Literal runat="server" ID="Literal1"></asp:Literal>

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;

public class MyObject
    public String MyProperty { get; set; }
public partial class ListViewButtonExample : System.Web.UI.Page
    protected void Page_Init(object sender, EventArgs e)
        ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);

    void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        Literal1.Text = "You clicked the " + (String)e.CommandArgument + " button";

    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            List<MyObject> col = new List<MyObject>();
            col.Add(new MyObject { MyProperty = "test 1" });
            col.Add(new MyObject { MyProperty = "test 2" });
            ListView1.DataSource = col;


Technorati Tags: ,

Published 08 December 2009 03:48 PM by Jeff Widmer
Filed under: , ,


# rhjiv said on 13 April, 2011 05:33 PM

Lots of sites said this couldn't be done. Following your instructions, I had it working in15 minutes. Thanks!

# Yessus said on 30 June, 2011 12:33 PM

Excellent!!! Tanks!!!.

# Adam said on 19 October, 2011 03:22 AM

Simple and effective. Thanks for this!

# Chris said on 08 November, 2011 01:51 PM

Is there any way you could provide this code in Visual Basic?  

# Hussam said on 06 March, 2012 05:28 AM

thanks so much for that it saved me from a lot of work

# nick zwinggi said on 03 July, 2012 07:50 PM

amazingly simple, yet brilliant. you should write for the msdn. they need simple.

# Ankita said on 30 August, 2012 05:58 AM

If i handle click event with javascript function,then it is not working..Any idea how to do this?

# jogewang said on 10 October, 2012 12:35 AM

Very good. thanks.

Leave a Comment




This Blog