OnItemCommand事件在ListView中没有触发

本文关键字:事件 ListView OnItemCommand | 更新日期: 2023-09-27 18:05:21

OnItemCommand事件火时,我click的按钮里面的ItemTemplate ?我试了些办法,但什么也没发生。我是不是忘了放什么东西了?我该怎么做才能修好它?

aspx页面:

<asp:ListView ID="ShowPostsListView" runat="server" OnItemDataBound="ShowPostsListView_ItemDataBound" OnItemCommand="ShowPostsListView_ItemCommand">
    <ItemTemplate>
      <asp:Button ID="AddCommentButton" CssClass="addCommentButton" runat="server" Text="Add Comment" CommandName="Add Comment" />
    </ItemTemplate>
</asp:ListView>

页面后代码:

protected void ShowPostsListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Add Comment")
    {
                 ...
    }
}

OnItemCommand事件在ListView中没有触发

另一种方法是为按钮添加OnClick事件处理程序,如下所示:

<asp:ListView ID="ShowPostsListView" runat="server"    
 OnItemDataBound="ShowPostsListView_ItemDataBound">
    <ItemTemplate>
      <asp:Button ID="AddCommentButton" CssClass="addCommentButton" 
        OnClick="addCommentButton_OnClick" runat="server" 
        Text="Add Comment"  />
    </ItemTemplate>
</asp:ListView>

然后:

protected void addCommentButton_OnClick(obejct sender, EventArgs e)
{
}

您是否将webconfig中的eventhandling设置为false以消除错误,如果您这样做了,请将其更改回相同的方式或使其为真,并在将数据绑定到listview JST添加

if(!isPostBack)
ListView1.Databind();

我希望这将解决问题,你的事件将触发