未将对象引用设置为对象的实例(NullreferenceException 未由用户代码处理)
我怎样才能解决这个异常?
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> </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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,在
DownloadsRepeater
控件中找不到名为imagepathlit
的控件,因此imagepathlit
控件在调用后为 null。请记住,
Control.FindControl()
根据ID
查找控件,而不是控件的名称。因此,要在集合中找到控件...您必须在应用程序的早期部分具有类似的内容:更新
由于您使用的是转发器,因此子控件的布局略有不同。
Repeater
中的每个Item
都会有一个Literal
实例。因此,要获取控件的每个实例,您必须循环遍历Repeater
中的Items
并在每个上调用
FindControl()
>项目:My guess is that there is no control found in the
DownloadsRepeater
control calledimagepathlit
, therefore theimagepathlit
control 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
Literal
for eachItem
in theRepeater
. Therefore, to get each instance of the control, you have to loop through theItems
in theRepeater
and callFindControl()
on eachItem
:假设您发布的代码确实引发了异常,我会说
DownloadRepeater
中没有 ID 为imagepathlit
的控件。检查您的
aspx
。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 ofimagepathlit
.Check your
aspx
.因为该控件位于 ItemTemplate 内,所以不能使用 Repeater.findcontrol;您必须循环遍历转发器的项目才能查找控件,因为项目模板是可重复的。因此,您必须循环遍历每个控件来查找控件,如下所示:
使用该语法。
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:
Use that syntax.