如何从中继器内部触发按钮事件

如何从中继器内部触发按钮事件

本文介绍了如何从中继器内部触发按钮事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了研究,但是找不到在 VB 中执行以下操作的有效方法:

I have done my research but can't find an efficient way to do the following in VB:

  • 每个按钮应触发同一事件.
  • 该按钮事件保存每个中继器项目,因此每个事件不是唯一.
  • Each button should fire the same event.
  • The button event saves every repeater item and so each event is not unique.

我知道我可以使用ItemCommand选项,但无法使其按预期工作.

I am aware I can use the ItemCommand option but have not been able to get it working as desired.

ASP.NET

内部中继器项目

<asp:Button ID="btnSave" RunAt="Server"/>

VB.NET

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    sqlConn.Open()
        For Each Item As RepeaterItem In rpt.Items
        ...
        Next
    sqlConn.Close()
End Sub

推荐答案

经过一些关于SO的研究后,我,除了ItemCommand以外没有其他事件被Asp:Repeater捕获,如 FlySwat 在回答中说.因此,您需要这样编写VB.NET代码:

After some research here on SO, I found that others events than ItemCommand are not caught by Asp:Repeater, as FlySwat said on his answer. So you'll need to write your VB.NET code like this:

首先,使用以下内容在页面上声明ItemCommand事件:

First, declare the ItemCommand event on your page with something like this:

Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
    If e.CommandName = "Save" Then
        'Save
    End If
End Sub

然后,必须在Asp:Repeater内的Asp:Button标记上,设置其CommandName属性,如下所示:

Then, on Asp:Button markup inside the Asp:Repeater, you must set its CommandName property like this:

<Asp:Button ID="btnSave" runat="server" CommandName="Save" UseSubmitBehavior="false"/>

看看此处以了解有关UseSubmitBehavior的更多信息.

Take a look here to learn more about the UseSubmitBehavior.

尝试一下.

这篇关于如何从中继器内部触发按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:46