Search code examples
asp.netlistviewlistviewitem

Find Unique Identifier From Selected Item In ASP ListView


I'm using a ListView and have two buttons that allows users to delete or edit a item from the list. Below is the code for the buttons:

<td>
   <asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png" ToolTip="Edit" OnClick="ButtonEdit_Click" />
   <asp:ImageButton ID="ButtonDelete" runat="server" ImageUrl="~/Styles/Images/Delete-Red-Cross.png" ToolTip="Delete" CommandName="Delete" />
 </td>

When the user clicks the edit button I want to pass the unique identifier from the item in the list to the parameter string so I can retrieve it in the edit page. Does anyone have any ideas on how to retrieve the ID from the selected item?


Solution

  • You could use the ImageButton.CommandArgument property:

    <td> 
       <asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png" ToolTip="Edit" OnClick="ButtonEdit_Click" CommandArgument='<%= ItemBeingOutput.ID %>'/> 
       <asp:ImageButton ID="ButtonDelete" runat="server" ImageUrl="~/Styles/Images/Delete-Red-Cross.png" ToolTip="Delete" CommandName="Delete" CommandArgument='<%= ItemBeingOutput.ID %>'/> 
     </td>