将转发器数据绑定到List- 找不到T的财产?
我有一个基本的转发器:
<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'.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 BlogItem 更改为:
您正在使用字段;它期望属性。
Change BlogItem to this:
You are using fields; it expects properties.