2

我怎样才能绕过这个异常?

Dim imagepathlit As Literal = DownloadsRepeater.FindControl("imagepathlit")
        imagepathlit.Text = imagepath

这是中继器:

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

这是获取转发器数据的代码:

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()
4

3 回答 3

1

我的猜测是在被调用的控件中没有找到DownloadsRepeater控件imagepathlit,因此imagepathlit调用后控件为空。

请记住,Control.FindControl()基于 查找控件ID,而不是控件的名称。因此,要在集合中找到控件......你必须在应用程序的早期有这样的东西:

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

更新

由于您使用的是中继器,因此子控件的布局会有所不同。您LiteralItemRepeater. 因此,要获取控件的每个实例,您必须遍历ItemsinRepeater并调用FindControl()each Item

For Each item As Item In DownloadsRepeater.Items
    Dim imagepathlit As Literal = item.FindControl("imagepathlit")
Next
于 2010-04-14T12:21:00.693 回答
1

假设您发布的代码确实是引发异常的地方,我会说其中DownloadRepeater没有 ID 为imagepathlit.

检查您的aspx.

于 2010-04-14T12:21:53.927 回答
1

因为控件在 ItemTemplate 中,所以不能使用 repeater.findcontrol;您必须遍历中继器的项目以查找控件,因为 itemtemplate 是可重复的。因此,您必须遍历每一个以查找控件,如下所示:

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

使用该语法。

于 2010-04-14T12:40:30.590 回答