如何访问ASP.net中嵌套转发器内的按钮?

时间:2016-06-13 14:24:19

标签: asp.net button nested repeater

我一直在努力了解正在发生的事情,我在网上搜索无济于事。

如何访问嵌套转发器内的按钮?我花了这么多时间研究/试验,我不明白发生了什么。

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时,我希望能够进入数据库并更新计数器,以便我可以跟踪该问题答案的投票数和投票数。< / p>

无论我实施什么样的更改,当我点击按钮时页面刷新就会尝试让它工作,没有别的。

1 个答案:

答案 0 :(得分:1)

HTML标记:您必须将OnItemCommand事件添加到内部转发器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")
    {
        ....
    }
}