将转发器数据绑定到List- 找不到T的财产?

发布于 2025-01-05 23:26:34 字数 1713 浏览 1 评论 0原文

我有一个基本的转发器:

<asp:Repeater id="BlogDisplay" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td valign="top" align="left" style="font-weight:bold; padding-left:3px;">
                <%# DataBinder.Eval(Container.DataItem, "Title")%>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>

</asp:Repeater>

当我执行代码时,出现异常:DataBinding: 'DocumentWebParts.BlogPostLost.BlogItem' does not contains a property with name 'Title'.

这让我不高兴

所以您可能认为我的 BlogItem 对象没有 Title财产——但事实并非如此!

public class BlogItem
{
    public int Id;
    public string Body;
    public string Title;
    public string Author;
    public DateTime Published;
}

并且该属性肯定已被设置 - 抛出断点表明所有值均已设置。

BlogItems = new List<BlogItem>();
SPListItem item;
foreach (SPListItem i in myItems)
            {
                //off-topic code hidden

                BlogItem b = new BlogItem();
                b.Id = Id;
                b.Body = Body;
                b.Author = Author;
                b.Title = Title;
                b.Published = Published;
                BlogItems.Add(b);
            }
            BlogItems = BlogItems.OrderByDescending(x => x.Id).ToList();
            BlogDisplay.DataSource = BlogItems;
            BlogDisplay.DataBind();

因此,考虑到所有这些,为什么当我在转发器中使用 DataBinder.Eval(Container.DataItem, "Title") 时会出现上述异常?

I have a basic repeater as such:

<asp:Repeater id="BlogDisplay" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td valign="top" align="left" style="font-weight:bold; padding-left:3px;">
                <%# DataBinder.Eval(Container.DataItem, "Title")%>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>

</asp:Repeater>

When I execute the code I get an exception: DataBinding: 'DocumentWebParts.BlogPostLost.BlogItem' does not contain a property with the name 'Title'.

This upsets me

So you're probably thinking that my BlogItem object doesn't have a Title property - but that's not the case!

public class BlogItem
{
    public int Id;
    public string Body;
    public string Title;
    public string Author;
    public DateTime Published;
}

And the property is certainly being set - throwing a breakpoint shows that all values have been set.

BlogItems = new List<BlogItem>();
SPListItem item;
foreach (SPListItem i in myItems)
            {
                //off-topic code hidden

                BlogItem b = new BlogItem();
                b.Id = Id;
                b.Body = Body;
                b.Author = Author;
                b.Title = Title;
                b.Published = Published;
                BlogItems.Add(b);
            }
            BlogItems = BlogItems.OrderByDescending(x => x.Id).ToList();
            BlogDisplay.DataSource = BlogItems;
            BlogDisplay.DataBind();

So in light of all of this, why am I getting the above exception when I use DataBinder.Eval(Container.DataItem, "Title") in the repeater?

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

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

发布评论

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

评论(1

江湖彼岸 2025-01-12 23:26:34

将 BlogItem 更改为:

public class BlogItem
{
    public int Id { get; set; }
    public string Body { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime Published { get; set; }
}

您正在使用字段;它期望属性。

Change BlogItem to this:

public class BlogItem
{
    public int Id { get; set; }
    public string Body { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime Published { get; set; }
}

You are using fields; it expects properties.

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