找不到类型包的默认成员

时间:2016-03-24 15:37:41

标签: vb.net webforms repeater

这是一个副本,如下所述,但我认为我们需要保留它。在发布之前,我确实在寻找帮助时找到了副本。这里的解释比我复制更容易。

当我运行下面的代码时,我在运行ParcelsRepeater.DataSource = Split.Parcels命令后立即收到错误“找不到类型包的默认成员”。错误消息似乎突出显示<%#Container.DataItem(“PIN”)%>在aspx代码中。

它似乎永远不会击中ItemDataBound事件。

Imports System.Linq


Public Class SplitCreateParcels
Inherits System.Web.UI.Page

Public Class SplitParcel

    Public Sub New()
        Parcels = New List(Of Parcel)
    End Sub


    Public Property Parcels As List(Of Parcel)

End Class

Public Class Cat
    Public Property Category As String
End Class

Public Class Parcel

    Public Sub New()
        Categories = New List(Of Cat)
    End Sub
    Public Property PIN As String
    Public Property Categories As List(Of Cat)
End Class

Public Property _Split As SplitParcel


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim Split As New SplitParcel
    Dim Parcel1 As New Parcel
    Dim Parcel2 As New Parcel


    Split.Parcels.Add(Parcel1)
    Split.Parcels.Add(Parcel2)

    Parcel1.PIN = "Test1"
    Parcel1.Categories.Add(New Cat With {.Category = "1"})
    Parcel1.Categories.Add(New Cat With {.Category = "2"})

    Parcel2.PIN = "Test2"
    Parcel2.Categories.Add(New Cat With {.Category = "3"})
    Parcel2.Categories.Add(New Cat With {.Category = "4"})

    ParcelsRepeater.DataSource = Split.Parcels
    ParcelsRepeater.DataBind()


End Sub

Protected Sub ParcelsRepeater_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim ParentDataItem As Parcel = DirectCast(e.Item.DataItem, Parcel)


        Dim ChildRepeater As Repeater = DirectCast(e.Item.FindControl("IntegerRepeater"), Repeater)
        ChildRepeater.DataSource = ParentDataItem.Categories
        ChildRepeater.DataBind()



    End If
End Sub
End Class



<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SplitCreateParcels.aspx.vb" Inherits="UADWeb.SplitCreateParcels" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Repeater ID="ParcelsRepeater" runat="server"  OnItemDataBound="ParcelsRepeater_ItemDataBound">
            <ItemTemplate>
                <%#Container.DataItem("PIN")%>
                <asp:Repeater runat="server" ID="IntegerRepeater">
                    <ItemTemplate>
                        <%#Container.DataItem("Category")%></br>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

我相信你的问题就在这一行:

<%#Container.DataItem("PIN")%>

这发生在ParcelsRepeater_ItemDataBound之前,因为它是作为数据绑定的一部分处理的。

正如您已在代码中展示的那样,您需要将DataItem强制转换为正确的类型以访问属性。以下应该有效:

<%#DirectCast(Container.DataItem, [Namespaced Path To your Type].Parcel).PIN%>

由于代码隐藏与标记处理代码的方式不同,因此必须提供Parcel类型的完整路径。要替代该语法,请can view this Q&A

作为一种不同的解决方案,您只需在项目模板中添加文字并在代码隐藏中填充文字:

<asp:Repeater ID="ParcelsRepeater" runat="server" 
  OnItemDataBound="ParcelsRepeater_ItemDataBound">
  <ItemTemplate>
    <asp:Literal Text="" ID="Pin" runat="server" /> 
    ...

然后使用ParcelsRepeater_ItemDataBound

Dim ParentDataItem = DirectCast(e.Item.DataItem, Parcel)
Dim PinLiteral = DirectCast(e.Item.FindControl("Pin"), Literal)
PinLiteral.Text = ParentDataItem.PIN
...