如何访问 ASP.net 中嵌套中继器内的按钮?
How to access a button inside a nested repeater in ASP.net?
我一直在努力了解发生了什么,但我在网上搜索无济于事。
如何访问嵌套中继器内的按钮?我花了这么多时间researching/experimenting,我不明白这是怎么回事。
HTML 标记:
<asp:Repeater runat="server" ID="repQuestionTopics">
....
<asp:Repeater ID="repQA" runat="server">
....
<strong>Was this article helpful?</strong>
<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")
{
....
}
}
我一直在努力了解发生了什么,但我在网上搜索无济于事。
如何访问嵌套中继器内的按钮?我花了这么多时间researching/experimenting,我不明白这是怎么回事。
HTML 标记:
<asp:Repeater runat="server" ID="repQuestionTopics">
....
<asp:Repeater ID="repQA" runat="server">
....
<strong>Was this article helpful?</strong>
<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")
{
....
}
}