在 Repeater 中查找兄弟控件

Find sibling control in Repeater

我有一个 Repeater 结构如下:

<asp:Repeater ID="rptListClaimTypes" runat="server">
    <ItemTemplate>
        <asp:FileUpload ID="fuContract" runat="server" />
        <asp:LinkButton ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" CommandName='<%# Eval("ClaimTypeID")%>' />
    </ItemTemplate>
</asp:Repeater>

我需要处理点击btnUpload时的文件上传。我可以使用 sender 访问触发子例程的控件。我将如何访问 fuContract

Protected Sub btnUpload_Click(sender As Object, e As EventArgs)

    Dim ClaimTypeID As Integer = sender.CommandName
    Dim fuContract As FileUpload = '??

End Sub

使用您当前的事件处理方法,您可以将发送方转换为 LinkBut​​ton,将父级转换为 RepeaterItem,然后使用 FindControl 查找 FileUpload 控件:

Dim fuContract As FileUpload = CType(CType(sender, LinkButton).Parent.FindControl("fuContract"), FileUpload)

我更喜欢使用 Repeater 的 ItemCommand 事件来处理这些类型的事件:

Private Sub rptListClaimTypes_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles rptListClaimTypes.ItemCommand

    Dim fuContract As FileUpload = CType(e.Item.FindControl("fuContract"), FileUpload)

End Sub