在Repeater中查找兄弟控件

时间:2015-10-28 14:02:46

标签: .net vb.net repeater findcontrol

我有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

1 个答案:

答案 0 :(得分:1)

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

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

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

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