views:

21

answers:

1

The following "FindControl" method fails to find the imgAd control. Any idea why? Could it be the Table that contains it? The intent of the table is to line things up in columns across rows.

<asp:Content ID="Content3" ContentPlaceHolderID="phPageContent" runat="Server">
    <asp:Repeater ID="repBanner" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Image ID="imgAd" runat="server" AlternateText="Panda Visa" ImageUrl="Images/AffiliateBanners/125%20by%20125.jpg" />
                </td>
                <td>
                    <asp:TextBox ID="txtHtml" runat="server" Columns="80" ReadOnly="True" Rows="7" TextMode="MultiLine"></asp:TextBox>
                </td>
                <td>
                     <asp:Button runat="server" Text="Copy HTML to Clipboard" OnClientClick="ClipBoard('txtHtml')" />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>


Protected Sub repBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repBanner.ItemDataBound

    Dim CurrentAd As Ad = CType(e.Item.DataItem, Ad)
    Dim RepeaterItem As RepeaterItem = e.Item

    Dim imgAd As Image = CType(RepeaterItem.FindControl("imgAd"), Image)
    imgAd.ImageUrl = "Images/" & "125 by 125.jpg" '<<<Error occurs here

End Sub

Object reference not set to an instance of an object.

Here's some debug info that I thought may help:

? RepeaterItem.Controls.Count
1
? RepeaterItem.Controls(0).Controls.Count
0
? typename(RepeaterItem.Controls(0))
"LiteralControl"
+1  A: 

You need to check e.Item.ItemType to make sure that you're dealing with an item, not a header or footer. Something like this:

Protected Sub repBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repBanner.ItemDataBound
    If (e.Item.ItemType <> ListItemType.Item AndAlso e.Item.ItemType <> ListItemType.AlternatingItem) Then
        Return
    End If

    Dim CurrentAd As Ad = CType(e.Item.DataItem, Ad)
    Dim RepeaterItem As RepeaterItem = e.Item

    Dim imgAd As Image = CType(RepeaterItem.FindControl("imgAd"), Image)
    imgAd.ImageUrl = "Images/" & "125 by 125.jpg" '<<<Error occurs here

End Sub
bdukes
I love this site
Velika