ImageButton in ASP.NET Repeater does not fire OnClick eventhandler

后端 未结 2 758
栀梦
栀梦 2021-01-16 10:35

I have an ImageButton inside a repeater control. I have attached an eventhandler to the OnClick event of the ImageButton. But when I click the ImageButton the event does not

相关标签:
2条回答
  • 2021-01-16 10:48

    Inside a repeater control, a button does not behave the same way as out.

    You need to set the "CommandName" property of the button and in the Repeater.ItemCommand event check for that command name and do your logic there.

    0 讨论(0)
  • 2021-01-16 10:58

    I had det same problem, and solved it by using av asp:Linkbutton with a with the "OnCommand" event. My markup and code behind is posted below.

    Markup:

    <asp:Repeater ID="rptRecipients" runat="server">
                <HeaderTemplate>
                    <table>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="LabelRecipient" Text='<%# Eval("Value")%>' runat="server"></asp:Label>
                        </td>
                        <td>
                        <asp:LinkButton OnCommand="lbRemove_Command"  CommandArgument='<%# Eval("Key")%>'
                                CommandName="Remove"  runat="server">
                            <asp:Image ImageUrl="~/Images/delete.png" runat="server" />
                        </asp:LinkButton>
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
    

    Code behind:

     protected void lbRemove_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Remove":
                    Recipients.Remove(e.CommandArgument.ToString());
                    rptRecipients.DataSource = Recipients;
                    rptRecipients.DataBind();
                    break;
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题