views:

912

answers:

3

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?

+2  A: 

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

Paul
+3  A: 

Hi

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.

Dal
Making some progress - after fixing the event args, the EVENTTARGET value is now in the form collection, and it is the image button in questions.
cdonner
+4  A: 

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;
    ....
}
rick schott
You are right, but like I said, the code works as is on other pages, and changing the event args type did not fix the problem. There must be something else on the page that breaks this.
cdonner
Is your OnClientClick doing anything other than a confirm?
rick schott
no, just popping the alert box.
cdonner
What happens if you completely remove the OnClientClick from the markup?
rick schott
None of this makes sense, so I will ask this question. Page_Load will fire first then your events, are you debugging far enough in the page lifecycle to get to your event?
rick schott
Yes I am, I have a breakpoint there that does not get hit. I think it has to to with calling Databind() on the grid, though. I have a popup window for editing the items. I am using Shadowbox in an IFrame, so this loads in a separate page, and I am not communicating back if something changed, so I just called Databind on the grid unconditionally. It seems that I cannot do that. Thanks for your help!
cdonner