[英]Why DataBinding can't find a property which exist?
我已經聲明了一個類,但是當我嘗試訪問它的成員時,出現以下錯誤:
數據綁定:'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) {
}
}
}
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>
我相信是因為它正在尋找名為Rang的文字屬性 。 您有一個名為Rang的字段 ,但這與屬性不一樣:
編輯:代碼示例
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;
}
}
}
字段和屬性是相關的,因為屬性為類的每個實例的“私有”字段數據提供了一個公共的“包裝器”機制。 但必須指出的是,它們是獨立的概念,並且不能互換。 僅具有字段聲明(在某些對象中也稱為成員)不會將其公開為屬性。 請注意@FrédéricHamidi所說的內容-文檔指出"value of the expression parameter must evaluate to a public **property**"
(強調我的意思)。
正如此摘錄直接來自Microsoft一樣 ,EVAL必須以某種方式具有屬性 。
希望有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.