中继器控制。使用跨行的表

发布于 2024-09-12 05:21:12 字数 1796 浏览 5 评论 0原文

以下“FindControl”方法无法找到 imgAd 控件。知道为什么吗?难道是包含它的表吗?该表的目的是将内容跨行排列在列中。

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

以下是一些我认为可能有帮助的调试信息:

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

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-09-19 05:21:12

您需要检查 e .Item.ItemType 确保您处理的是项目,而不是页眉或页脚。像这样的事情:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文