0

I have a Repeater structured something like this:

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

I need to handle the file upload when btnUpload is clicked. I can access the control that triggered the subroutine with sender. How would I access fuContract?

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

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

End Sub
Turnip
  • 35,836
  • 15
  • 89
  • 111

1 Answers1

1

Using your current method of Event handling you would cast the sender as a LinkButton, cast the parent as a RepeaterItem, and then use FindControl to find the FileUpload Control:

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

I prefer handling these types of Events using the Repeater's ItemCommand Event though:

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
NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • That's great. I did just work it out using a slightly long winded way `Dim btnUpload As LinkButton = DirectCast(sender, LinkButton)` `Dim rpItem As RepeaterItem = TryCast(btnUpload.NamingContainer, RepeaterItem)` `Dim fuContract As FileUpload = rpItem.FindControl("fuContract")` but your one-liner is much better. Many thanks. – Turnip Oct 28 '15 at 14:40