简体   繁体   English

如何从转发器内部触发按钮事件?

[英]How to fire a button event from inside a repeater?

I have done my research but can't find an efficient way to do the following in VB : 我已经完成了我的研究但是找不到在VB中执行以下操作的有效方法:

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

I am aware I can use the ItemCommand option but have not been able to get it working as desired. 我知道我可以使用ItemCommand选项,但无法使其按预期工作。

ASP.NET ASP.NET

Inside Repeater Item 内置中继器项目

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

VB.NET 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

Edit: 编辑:

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的一些研究后,我发现其他事件而不是ItemCommand并没有被Asp:Repeater捕获,正如FlySwat在回答中说的那样。 So you'll need to write your VB.NET code like this: 所以你需要编写你的VB.NET代码:

First, declare the ItemCommand event on your page with something like this: 首先,在页面上声明ItemCommand事件,如下所示:

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

Then, on Asp:Button markup inside the Asp:Repeater , you must set its CommandName property like this: 然后,在Asp:Button里面标记Asp:Repeater ,你必须设置它CommandName属性是这样的:

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

Take a look here to learn more about the UseSubmitBehavior . 看一下这里了解有关UseSubmitBehavior更多信息。

Try it. 试试吧。

When the button is inside a Repeater template, you need to add OnClick event, you can add event on ItemDataBound event of the Repeater control. 当按钮位于Repeater模板内时,您需要添加OnClick事件,您可以在Repeater控件的ItemDataBound event上添加事件。

Your .aspx code will look something like this: 你的.aspx代码看起来像这样:

 <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Button  ID="btnSave" runat="server" Text="SomeText" />
    </ItemTemplate>
</asp:Repeater>

code-behind 代码隐藏

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
    {
        var btn = e.Item.FindControl("btnSave") as Button;
        if (btn != null)
        {  // adding button event 
            btn.Click += new EventHandler(btn_Click);
        }
    }
}

void btn_Click(object sender, EventArgs e)
{
 //write your code 
}

in vb.net 在vb.net中

Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemType = Repeater1.AlternatingItem OrElse e.Item.ItemType = Repeater1.Item Then
        Dim btn = TryCast(e.Item.FindControl("btnSave"), Button)
        If btn IsNot Nothing Then
            ' adding button event 
            btn.Click += New EventHandler(btn_Click)
        End If
    End If
End Sub

Private Sub btn_Click(sender As Object, e As EventArgs)
    'write your code 
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM