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 sender
to the Button. The Button's NamingContainer
is the ListViewItem
. You can use this to get all your other control in that item by using item.FindControl("OtherControlID")
.
For example;
public void delete_Onclick(object sender, EventArgs e) {
var btn = (Button)sender;
var item = (ListViewItem)btn.NamingContainer;
// find other controls:
var btnModify = (Button)item.FindControl("modify");
}
You cannot find text that is not in a server control. But you could use a LiteralControl
or Label
to display the username or email.
<td><asp:Label id="Label1" runat="server"
Text='<%# Eval("user.UserName") %>' />
</td>
Now you can use FindControl
as shown above to get a reference to the label in codebehind.
I know 2 solutions:
1-command name and command argument as below
2-you can use links instead of buttons and send queryString that include the id of your record to the same page u are,and take querystring in code behind(page load) and use it for delete sqlcommadns or redirect it to another page for editing purposes and finally Response.Redirect() again to the same page that you are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With