ASP.NET ImageButton Onclick Event not handled
Solution 1
Your event is defined incorrectly ImageButton.Click:
protected void btnDeleteAccountItem_Click(object sender, ImageClickEventArgs e) {
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
....
}
Solution 2
I'm not sure if this will solve it, but once I placed a asp:Button control in my markup and generated the 'onClick' signature for it too.
I then changed my mind and decided to make it an Image button... ... I simply rewrote the tag myself.
After making those changes, I realised that the onClick signature wasn't working anymore... after some research I found an answer... I was using 'EventArgs' instead of 'ImageClickEventArgs'...
(object sender, ImageClickEventArgs e)
Once I changed the event arg object, it started working as normal.
Solution 3
rather than creating a button click event, you could use the datagrid row command event
You can then use e.commandName and e.commandArgument to find out which button was pressed and what its argument is:
Private Sub gv1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv1.RowCommand
If e.CommandName = "Whatever" Then
// do something
End If
Hope it helps
Solution 4
make sure the CausesValidation is set to True.
cdonner
„Dem Herrn Ingeniör ist nichts zu schwör." At pod, We are aways looking for new and interesting projects.
Updated on June 05, 2022Comments
-
cdonner almost 2 years
I have an ImageButton in a GridView.
<asp:GridView ID="ItemGridView" runat="server" DataKeyNames="Id" OnRowDataBound="ItemGridView_RowDataBound" AllowPaging="True" AllowSorting="True" EnableSortingAndPagingCallbacks="True" AutoGenerateEditButton="False" AutoGenerateDeleteButton="false" DataSourceID="ItemDataSource" EnableViewState="true" > .... <asp:TemplateField ShowHeader="False" ItemStyle-Width="40px"> <ItemTemplate> <asp:ImageButton ID="btnDelete" SkinID="btnDelete" runat="server" CausesValidation="false" OnClick="btnDeleteAccountItem_Click" OnClientClick="javascript:return confirm('Are you sure?');" /> </ItemTemplate> </asp:TemplateField>
and a corresponding handler for the delete button event
protected void btnDeleteAccountItem_Click(object sender, EventArgs e) { ImageButton btnDel = sender as ImageButton; GridViewRow row = (GridViewRow)btnDel.NamingContainer; .... }
I am using this very same construct in many places and it works fine. I have one gridview now, though, where it does not, and I am hoping to get some ideas for how to track down the problem. When I click the button, the client-side event fires and the alert box is displayed, then the post-back fires and I can hit a break point in the
Page_Load
method. So the client-side wiring of the button events seems to work. However, the event is not handled and the method btnDeleteAccountItem_Click does not get called.This is a complex page and I cannot post all the code. What can I do to narrow down potential causes?