c# - "DataBinding: ' index+NewsItem ' does not contain a property with the name ' 链接 '",但属性存在(不是错字)

标签 c# .net asp.net .net-3.5

首先我有一个转发器,我正在输出一个属性。我都试过了

<%#Eval("Link")%>

<%#DataBinder.Eval(Container.DataItem, "Link")%>

我有一个简单的类

public class NewsItem  
{  
    public string Link = "";  
    public string Title = "";  
}  

我用一个简单的 for...each 填充列表新闻,然后...

repeater.DataSource = news;
repeater.DataBind();

我得到“DataBinding:‘index+NewsItem’不包含名为‘Link’的属性

最佳答案

您创建的是字段,而不是属性

要将它们变成属性,请像这样调整您的代码

public class NewsItem  
{  
     public string Link { get; set; }
     public string Title { get; set; }              

     public NewsItem()
     {
           this.Link = string.Empty;
           this.Title = string.Empty;
     }
}  

关于c# - "DataBinding: ' index+NewsItem ' does not contain a property with the name ' 链接 '",但属性存在(不是错字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7271456/

相关文章:

c# - DLL 引用未复制到项目 bin 中

c# - 如何处理 ASP.NET MVC 站点上长时间运行的进程?

c# - 执行Response.Redirect时如何修改Request.Headers ["Referer"]?

c# - 实现 IDisposable - 一次性字段与一次性属性

c# - 在 ASP.NET Web 应用程序之外获取 UserManager 实例?

c# - 使用 .net C# 在 SNMP 项目上编码错误

c# - 对类类型操作数使用 == 运算符

asp.net - 复合服务器控件(包括用户控件、自定义服务器控件)是否通常封装 css 属性?

c# - 泛型协变和显式转换

c# - 如何 reshape 形态?