I have an html button and I’d like to have a server side click event but it’s not firing when the button is clicked and the page does a postback. The button is inside the ItemTemplate for an <asp:ListView id="usersListView"/> that renders a table of information.
aspx
<button runat="server" id="delete" class="btn btn-mini" title="Delete" OnServerClick="delete_Onclick"></button>
aspx.cs
protected void delete_Onclick(object sender, EventArgs e) {
ListViewItem listViewItem = (ListViewItem)((Button)sender).NamingContainer;
if(listViewItem != null) {
Membership.DeleteUser(((Label) listViewItem.FindControl("userName")).Text);
}
}
My guess is that it will not work like this because events raised by nested controls placed in item templates should rather be handled by ListView’s
ItemCommandevent.For this to work then, you should set button’s
CommandNameandCommandArgumentand handle specific values in the listview’sItemCommand.However, if I remember correcly, the
HtmlInputButtondoes not haveCommandNameandCommandArgumentproperties. Instead, useasp:Buttonand handle the listview’s itemcommand:
Note that
CommandArgumentis usually bound to an item-specific value (id perhaps) so that inside the server-side handler you can precisely identify the exact clicked button: