简体   繁体   English

OnItemCommand事件不会在ListView中触发

[英]OnItemCommand Event Doesn't Fire In ListView

OnItemCommand event doesn't fire when I click the button inside ItemTemplate ? 当我click 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: 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>

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: 另一种执行相同操作的方法是按如下方式向按钮添加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>

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 您是否在webconfig中将事件处理设置为false以消除错误,如果这样做,请将其更改回相同的方式或将其设为true,并在将数据绑定到listview之前添加

if(!isPostBack)
ListView1.Databind();

i hope this will solve the problem, and your event will fire 我希望这能解决问题,并且您的活动将会触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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