开发者

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 am using this very same construct in many places and it wo开发者_JAVA技巧rks 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?


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;
    ....
}


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.


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


make sure the CausesValidation is set to True.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜