我有一个基本的转发器:
<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 contain a property with the 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")
时会遇到上述异常?
答案 0 :(得分:9)
将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; }
}
你正在使用田地;它期望属性。