views:

217

answers:

3

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>&nbsp;</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()
+1  A: 

My guess is that there is no control found in the DownloadsRepeater control called imagepathlit, therefore the imagepathlit control is null after the call.

Remember that Control.FindControl() looks up the control based on ID, 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:

Dim imagepathlit As Literal = new Literal()
imagepathlit.ID = "imagepathlit"

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 Literal for each Item in the Repeater. Therefore, to get each instance of the control, you have to loop through the Items in the Repeater and call FindControl() on each Item:

For Each item As Item In DownloadsRepeater.Items
    Dim imagepathlit As Literal = item.FindControl("imagepathlit")
Next
Justin Niessner
The literal is in the .aspx like this; <asp:Literal ID="imagepathlit" runat="server"></asp:Literal>
Phil
Thanks a lot Justin
Phil
+1  A: 

Assuming the code you posted is where the exception is indeed thrown, I would say that the DownloadRepeater does not have a control in it that has an ID of imagepathlit.

Check your aspx.

Oded
+1  A: 

Hey,

Because the control is within the ItemTemplate, you cannot use repeater.findcontrol; you have to loop through the items of the repeater to look for the control, as the itemtemplate is repeatable. So you have to loop through each one to look for the control as in:

foreach (var item in repeater.Items)
{
   var control = item.FindControl("ID") as Type;
}

Use that syntax.

Brian