How can I get around this exception?
Dim imagepathlit As Literal = DownloadsRepeater.FindControl("imagepathlit")
imagepathlit.Text = imagepath
Here is the repeater:
<asp:Repeater ID="DownloadsRepeater" runat="server">
<HeaderTemplate>
<table width="70%">
<tr>
<td colspan="3"><h2>Files you can download</h2></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="5%">
<asp:Literal ID="imagepathlit" runat="server"></asp:Literal></td>
<td width="5%"></td>
<td> </td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Here is the code that gets the data for the repeater:
c.Open()
r = x.ExecuteReader
While r.Read()
If r("filename") Is DBNull.Value Then
imagepath = String.Empty
Else
imagepath = "<img src=images/" & getimage(r("filename")) & " border=0 align=absmiddle>"
End If
End While
c.Close()
r.Close()
My guess is that there is no control found in the
DownloadsRepeatercontrol calledimagepathlit, therefore theimagepathlitcontrol is null after the call.Remember that
Control.FindControl()looks up the control based onID, not the name of the control. Therefore, to find the control in the collection…you would have to had something like this earlier in the application:UPDATE
Since you’re using a repeater, the child controls get layed out a bit differently. You’re going to have an instance of the
Literalfor eachItemin theRepeater. Therefore, to get each instance of the control, you have to loop through theItemsin theRepeaterand callFindControl()on eachItem: