ASP.NET ImageButton Onclick Event Not Handled
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 use this very construction in many places and it works great. I now have one gridview, but it doesn't, and I'm hoping to get some ideas on how to track down the problem. When I click the button, a client side event fires and an alert box pops up, then the feedback fires and I can hit a breakpoint in the method Page_Load
. So the client side button interaction seems to work. However, the event is not handled and the btnDeleteAccountItem_Click method is not called.
This is a tricky page and I cannot post all the code. What can I do to narrow down the potential causes?
source to share
Your event is not defined correctly ImageButton.Click :
protected void btnDeleteAccountItem_Click(object sender, ImageClickEventArgs e) {
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
....
}
source to share
I'm not sure if this will solve it, but once I placed the asp: Button control in my markup and generated the 'onClick' signature for it too.
Then I changed my mind and decided to make it an Image button ...... I just rewrote the tag myself.
After making these changes, I realized that the onClick signature no longer works ... after some research I found the answer ... I used "EventArgs" instead of "ImageClickEventArgs" ...
(object sender, ImageClickEventArgs e)
As soon as I changed the event arg object, it started working as usual.
source to share
instead of creating a button click event, you can use the datagrid row command event
Then you can use e.commandName and e.commandArgument to find out which button was clicked 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
source to share
You can try (you are creating the wrong click event for the ImageButton):
<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 for the image button, there is a change in the click event as shown below:
protected void btnDelete_Click(object sender, ImageClickEventArgs e)
{
ImageButton btnDel = sender as ImageButton;
GridViewRow row = (GridViewRow)btnDel.NamingContainer;
}
source to share