Repeater ItemCommand doesn't work

本文关键字:work ItemCommand doesn Repeater | 更新日期: 2023-09-27 18:04:42

我在webform中OnItemCommand事件不工作的中继器中遇到麻烦。当我点击链接按钮时,他应该被解雇了。

脏污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> 
c# 背后的代码
protected void repeaterImagens_ItemCommand(object source, RepeaterCommandEventArgs e)
{
     if (e.CommandName.Equals("excluir"))
    {
           ExcluirArquivo(e.CommandArgument.ToString());
     }
}

在调试模式下测试,点击Linkbutton什么都没有发生,甚至没有调用ItemCommand事件

Repeater ItemCommand doesn't work

更好的方法是在ItemDataBound事件中处理linkbutton客户端确认:

 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?')";
     }
 }