2

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?

4
  • Have you checked any info in Page_Load, such as whether IsPostBack is true or the EVENTTARGET value in the form collection? Commented Oct 20, 2009 at 15:48
  • IsPostBack is true and the form collection appears to be empty. What does that mean? Commented Oct 20, 2009 at 16:01
  • I should add that the GridView is on a MultiView control, and that the form tag is in the master page. Commented Oct 20, 2009 at 16:18
  • See below - the EVENTTARGET now has the correct value, it seems, but still no invocation of the handler. Commented Oct 20, 2009 at 17:07

5 Answers 5

5

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;
    ....
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Is your OnClientClick doing anything other than a confirm?
What happens if you completely remove the OnClientClick from the markup?
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?
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!
3

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.

1 Comment

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.
2

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

Comments

1

make sure the CausesValidation is set to True.

Comments

0

You can try (you are creating wrong click event of 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 image button there are change in click event like as follow :

protected void btnDelete_Click(object sender, ImageClickEventArgs e)
{
 ImageButton btnDel = sender as ImageButton;
 GridViewRow row = (GridViewRow)btnDel.NamingContainer;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.