未将对象引用设置为对象的实例(NullreferenceException 未由用户代码处理)

发布于 2024-08-29 00:58:38 字数 1117 浏览 10 评论 0原文

我怎样才能解决这个异常?

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

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 技术交流群。

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

发布评论

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

评论(3

亽野灬性zι浪 2024-09-05 00:58:38

我的猜测是,在 DownloadsRepeater 控件中找不到名为 imagepathlit 的控件,因此 imagepathlit 控件在调用后为 null。

请记住,Control.FindControl() 根据 ID 查找控件,而不是控件的名称。因此,要在集合中找到控件...您必须在应用程序的早期部分具有类似的内容:

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

更新

由于您使用的是转发器,因此子控件的布局略有不同。 Repeater 中的每个 Item 都会有一个 Literal 实例。因此,要获取控件的每个实例,您必须循环遍历 Repeater 中的 Items 并在每个 上调用 FindControl() >项目:

For Each item As Item In DownloadsRepeater.Items
    Dim imagepathlit As Literal = item.FindControl("imagepathlit")
Next

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
末が日狂欢 2024-09-05 00:58:38

假设您发布的代码确实引发了异常,我会说 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 of imagepathlit.

Check your aspx.

顾冷 2024-09-05 00:58:38

因为该控件位于 ItemTemplate 内,所以不能使用 Repeater.findcontrol;您必须循环遍历转发器的项目才能查找控件,因为项目模板是可重复的。因此,您必须循环遍历每个控件来查找控件,如下所示:

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

使用该语法。

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.

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