简体   繁体   中英

OnItemCommand Event Doesn't Fire In ListView

OnItemCommand event fire when I click the button inside ItemTemplate ? 触发OnItemCommand事件? I tried something but nothing happened. Did I forgot to put something or something else ? What should I do to fix it?

aspx page:

<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>

code behind page:

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

Another way to do the same thing would be to add an OnClick event handler to the button as so:

<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>

And then:

protected void addCommentButton_OnClick(obejct sender, EventArgs e)
{

}

have you set the eventhandling in webconfig false to get rid of the error, if you did that please change it back to same way or make it true, and before binding the data to your listview jst add

if(!isPostBack)
ListView1.Databind();

i hope this will solve the problem, and your event will fire

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