I have a ListView that creates a table. Each row in the table has two buttons – Delete and Modify. I’m firing a click event for each button but I’m not sure how to get a handle to the data in the row that the button was clicked.
aspx
<asp:ListView runat="server" ID="usersListView">
<LayoutTemplate>
<table class="table table-striped">
<thead>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"/>
</tbody>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"/>
</tr>
</GroupTemplate>
<ItemTemplate>
<td><%# Eval("user.UserName") %></td>
<td><%# Eval("user.Email") %></td>
<td><%# Eval("roles") %></td>
<td>
<button runat="server" id="modify" class="btn btn-mini" title="Modify" onclick="modify_OnClick">
<i class="icon-edit"></i>
</button>
<button runat="server" id="delete" class="btn btn-mini" title="Delete" onclick="delete_OnClick">
<i class="icon-trash"></i>
</button>
</td>
</ItemTemplate>
</asp:ListView>
aspx.cs
public void delete_Onclick(object sender, EventArgs e) {
}
public void modify_Onclick(object sender, EventArgs e) {
}
I will try to answer the question in the title since i don’t understand the question itself.
You can cast the the
senderto the Button. The Button’sNamingContaineris theListViewItem. You can use this to get all your other control in that item by usingitem.FindControl("OtherControlID").For example;
You cannot find text that is not in a server control. But you could use a
LiteralControlorLabelto display the username or email.Now you can use
FindControlas shown above to get a reference to the label in codebehind.