简体   繁体   中英

Repeater ItemCommand doesn't work

I'm having trouble in a Repeater in webform where OnItemCommand event is not working. He should be fired when I click the Linkbutton.

Codigo aspx:

<asp:Repeater ID="repeaterImagens" runat="server" 
        OnItemCommand="repeaterImagens_ItemCommand" 
        OnItemDataBound="repeaterImagens_ItemDataBound">
       <ItemTemplate>

...

                <asp:LinkButton ID="lbExcluir" runat="server"
                        CommandName="excluir"
                        CommandArgument="<%# ((String)Container.DataItem) %>" 
                        OnClientClick="if (!confirm('Confirma a exclusão desta imagem?'));">
                </asp:LinkButton>
       </ItemTemplate>
</asp:Repeater> 

Code behind C#

protected void repeaterImagens_ItemCommand(object source, RepeaterCommandEventArgs e)
{
     if (e.CommandName.Equals("excluir"))
    {
           ExcluirArquivo(e.CommandArgument.ToString());
     }
}

Tested in debug mode, clicking the Linkbutton nothing happens, not even to call the ItemCommand event

Better way is to handle linkbutton client confirmation in ItemDataBound event:

 protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {
    LinkButton lb = e.Item.FindControl("lbExcluir") as LinkButton;

    if (lb != null) {
       lb.OnClientClick = "return confirm('Confirma a exclusão desta imagem?')";
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM