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" >
    <ItemTemplate>
        <asp:Button runat="server" Text='<%# Eval("MyProperty") %>' CommandArgument='<%# Eval("MyProperty") %>' /><br />
    </ItemTemplate>
</asp:ListView>

image

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

image

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:

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

ListViewButtonExample.aspx.cs:
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;
            ListView1.DataBind();
        }
    }
}

 

Technorati Tags: ,


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

Comments

# 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

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

News

Syndication