You want an ItemCommand
event. You give your button the command name, command argument (if you want), and then listen to the ItemCommand event in the ListView
<asp:ListView ID="ListView1" runat="server" OnItemCommand="DoTheCommand" DataSourceID="sqldatasource1"> <ItemTemplate> <asp:Button CommandName="Foo" CommandArgument='<%#Eval("SomeDataBoundProperty")%>' ID="ButtonTest" runat="server" BackColor="Silver" Text="Add to Cart" /> </ItemTemplate> </asp:ListView>
And in your code behind:
void DoTheCommand(object sender, ListViewCommandEventArgs e) { string commandName = e.CommandName; object commandArg = e.CommandArgument; ListViewItem selectedItem = e.Item; int dataItemIndex = selectedItem.DataItemIndex; if (commandName == "Foo") {
source share