asp.net - 如何访问 ASP.net 中嵌套中继器内的按钮?

标签 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 的访问权限?出于某种原因,我的代码隐藏不允许我编写函数 Handles repQA.ItemCommand - 为什么?我试图通过使用类似(添加 OnClick)的方法直接在 asp 端输入函数:

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

当单击按钮 voteYes 时,我希望能够转到数据库并更新计数器,以便我可以跟踪该答案的赞成票和反对票的数量问题。

无论我实现什么更改来尝试让它工作,当我单击按钮时,页面都会刷新,仅此而已 - 没有别的。

最佳答案

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

关于asp.net - 如何访问 ASP.net 中嵌套中继器内的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37792258/

相关文章:

c# - 如何在使用 AsyncFileUpload 时在客户端获取服务器端重命名文件名

ios - 为什么 UILongPressGestureRecognizer 在 iOS 模拟器中工作但不适用于 iPhone

python - 在 python 中迭代嵌套列表的更简单方法是什么?

asp.net - 命令 "nuget pack..."以代码 1 退出

asp.net - 如何获取网格控件中存在的文本框控件的 clientID

javascript - 在 asp.net 中重新加载弹出窗口

css - 有没有办法在 Internet Explorer 11 上使用嵌套媒体查询?

javascript - 即使聚焦并且 CSS 规则生效,HTML 按钮也不会单击

html - 无法对齐多个按钮上的文本/链接 - 菜鸟级别

java - 将数据存储到不同的对象但被覆盖