Moh*_*kar 0 c# asp.net data-binding
我已经声明了一个类但是当我尝试访问它的成员时,我收到以下错误:
DataBinding:'reapTest.Toop'不包含名为'Rang'的属性.
WebForm1.aspx.cs:
namespace reapTest {
public class Toop {
public string Rang;
public int Gheymat;
}
public static class MyData {
public static Toop[] TP = new Toop[] { new Toop() { Rang = "Ghermez", Gheymat = 100 }, new Toop() { Rang = "Yellow", Gheymat = 44 } };
public static Toop[] RT() {
return TP;
}
}
public partial class WebForm1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="reapTest.WebForm1" %>
<!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="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<%#Eval("Rang")%>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource runat="server" ID="ObjectDataSource1" SelectMethod="RT" TypeName="reapTest.MyData"></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我相信这是因为它正在寻找一个名为Rang 的文字属性.你有一个名为Rang 的字段,但这与一个属性不同,to-wit:
编辑:代码示例
public class Toop {
// These values are *fields* within the class, but *not* "properties."
private string m_Rang; // changing these field decls to include m_ prefix for clarity
private int m_Gheymat; // also changing them to private, which is a better practice
// This is a public *property* procedure
public string Rang
{
get
{
return m_Rang;
}
set
{
m_Rang = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
字段和属性是相关的,属性为类的每个实例的"私有"字段数据提供公共"包装"机制.但重要的是要注意它们是独立的概念,而不是可互换的.仅仅具有字段声明(在某些对象用语中也称为成员)不会将其作为属性公开.请注意@FrédéricHamidi所说的内容 - 文档说明了"value of the expression parameter must evaluate to a public **property**"(强调我的).
正如本摘录中直接提到的那样,EVAL,不管怎样,必须拥有一个属性.

希望这会有所帮助.