I currently have a LinkButton in the ItemTemplate of a ListView. Each button in the ListView should call the same click event handler. However, in the handler I need to know which button was clicked. Is this possible?
<asp:ListView runat="server" ID="lvKeyGroup">
<LayoutTemplate>
<table>
<asp:Placeholder runat="server" ID="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>[<asp:LinkButton runat="server" Text="Remove" OnClick="lbRemoveAuthGroup_Click" />]</td>
<td><%# Eval("AuthorizationGroup") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Add CommandName property to each LinkButton and handle ListView’s ItemCommand event.
Also you need to set ListView’s DataKeys property to your datasource object unique identifier name. The you can get selected row datakey:
void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// in assumption that your data item's unique identifier type is Int32
var dataKey = (int)ListView1.DataKeys[e.Item.DataItemIndex].Value;
switch(e.CommandName)
{
case "Remove":
// your code here
break;
}
}
Follow this link for ListView control overview: http://msdn.microsoft.com/en-us/library/bb398790.aspx
Also, watch this video: http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=dan-wahlin&name=webforms-03&mode=live&clip=0&course=aspdotnet-webforms4-intro
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0