简体   繁体   English

为什么DataBinding找不到存在的属性?

[英]Why DataBinding can't find a property which exist?

I have declared a class but when I try to access it's members I get the following error : 我已经声明了一个类,但是当我尝试访问它的成员时,出现以下错误:
DataBinding: 'reapTest.Toop' does not contain a property with the name 'Rang'. 数据绑定:'reapTest.Toop'不包含名称为'Rang'的属性。

WebForm1.aspx.cs : 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) {

        }
    }
}

WebForm1.aspx : 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>

I believe it is because it is looking for a literal property named Rang. 我相信是因为它正在寻找名为Rang的文字属性 You have a field named Rang, but that's not the same as a property, to-wit: 您有一个名为Rang的字段 ,但这与属性不一样:

EDIT: Code sample 编辑:代码示例

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;
         }
     }
}

Fields and Properties are related in that Properties provide a public "wrapper" mechanism to the "private" field data of each instance of the class. 字段和属性是相关的,因为属性为类的每个实例的“私有”字段数据提供了一个公共的“包装器”机制。 But it is critical to note that they are separate concepts, and not interchangeable. 但必须指出的是,它们是独立的概念,并且不能互换。 Merely having a field declaration (also called a member in some object parlance) does not expose it as a property. 仅具有字段声明(在某些对象中也称为成员)不会将其公开为属性。 Note what @FrédéricHamidi said - the docs state the "value of the expression parameter must evaluate to a public **property**" (emphasis mine). 请注意@FrédéricHamidi所说的内容-文档指出"value of the expression parameter must evaluate to a public **property**" (强调我的意思)。

As noted in this excerpt directly from Microsoft , EVAL, one way or the other, has to have a property . 正如此摘录直接来自Microsoft一样 ,EVAL必须以某种方式具有属性

在此处输入图片说明

Hopefully that helps. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM