2013-03-23 78 views
0

OnItemCommand事件不會當我點擊click按鈕時ItemTemplate?我嘗試了一些,但沒有發生。我忘了放東西或其他東西嗎?我該怎麼做才能解決它?OnItemCommand事件不會在ListView中觸發

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") 
    { 
       ... 
    } 
} 
+0

我修正了將UseSubmitBehavior =「false」'添加到按鈕控制屬性。我不知道爲什麼這會解決問題。如果有人能解釋它會很好。 – theChampion 2013-03-23 13:39:21

回答

0

另一種方式做同樣的事情將是一個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) 
{ 

} 
+0

這是一個替代,但沒有解釋這個問題 – tariq 2013-03-23 13:49:07

0

是否設置在webconfig假eventhandling擺脫錯誤的,如果你這樣做,請改回同樣的方式或使其真實的,數據綁定到你的列表視圖前JST添加

if(!isPostBack) 
ListView1.Databind(); 

我希望這將解決問題,並且你的事件會觸發