bflemi3
bflemi3

Reputation: 6790

Get ListView item when button in row is clicked

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>&nbsp;
            <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) {

}

Upvotes: 5

Views: 11251

Answers (3)

asp.net developer
asp.net developer

Reputation: 81

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.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460118

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.

Upvotes: 11

Roland Mai
Roland Mai

Reputation: 31077

You should be using a GridView since the data is tabular. At any rate, you can implement the ItemCommand event. The MSDN page has an example.

You can customize CommandName and the CommandArgument properties of an Button control. The CommandArgument can be made to contain some ID or a relevant piece of information.

Upvotes: 0

Related Questions