2016-06-13 75 views
2

我一直在試圖瞭解發生了什麼,並且我在網上搜索無濟於事。如何訪問ASP.net中嵌套中繼器內的按鈕?

如何訪問嵌套中繼器內的按鈕?我花了很多時間研究/試驗,我不明白髮生了什麼。

HTML標記:

<asp:Repeater runat="server" ID="repQuestionTopics"> 
    .... 
    <asp:Repeater ID="repQA" runat="server"> 
     .... 
     <strong>Was this article helpful?</strong>&nbsp; 
     <button class="button tiny secondary radius" runat="server" 
       CommandName="voteYes" id="btnVoteHelpfulYes">Yes</button> 
     <button class="button tiny secondary radius" runat="server" 
       CommandName="voteNo" ID="btnVoteHelpfulNo">No</button> 
    </asp:Repeater> 
</asp:Repeater> 

代碼隱藏:

//Handles repQuestionTopics.ItemCommand 
Public Sub repTopics_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs) 
    If e.CommandName = "voteYes" Then 
     .... 
    End If 
End Sub 

如何獲得對repQA訪問?出於某種原因,我的代碼不會讓我寫功能處理repQA.ItemCommand - 爲什麼?我曾嘗試直接輸入使用類似(添加的OnClick)上的ASP方面的功能:

<button class="button tiny secondary radius" runat="server" OnClick="repTopics_ItemCommand" 
     CommandName="voteYes" id="btnVoteHelpfulYes">Yes</button> 

當按鈕voteYes點擊我希望能夠去到數據庫並更新計數器,所以我可以跟蹤該問題的答案有多少票和反對票。

無論我實施什麼樣的變化,當我點擊按鈕來嘗試並使其工作,頁面刷新,就是這樣 - 沒有別的。

回答

0

HTML標記:你必須OnItemCommand事件添加到您的內部Repeater repAQ

<asp:Repeater ID="repQA" runat="server" OnItemCommand="repQA_ItemCommand"> 

代碼隱藏:

VB.Net:

Public Sub repQA_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs) 
    // check for right name of command 
    If e.CommandName = "voteYes" Then 
     .... 
    End If 
End Sub 

ASP.Net:

protected void repQA_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 
    // check for right name of command 
    if(e.CommandName == "voteYes") 
    { 
     .... 
    } 
}