且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GridView 绑定了嵌套类的属性

更新时间:2023-01-17 08:12:41

BoundField 列中只能显示实例的直接属性.

Only immediate properties of an instance can be displayed in a BoundField column.

必须在 itemtemplate 中使用 DataBinder.Eval 来访问嵌套属性,而不是将其分配给 boundfield.

One must instead use DataBinder.Eval in an itemtemplate to access the nested property instead of assigning it to a boundfield.

示例:

<asp:TemplateField>
    <itemtemplate>
        <p><%#DataBinder.Eval(Container.DataItem, "NestedClass.Name")%></p>
    </itemtemplate>
</asp:TemplateField>

或者,您可以创建一个自定义类,该类继承 BoundField 并覆盖 GetValue 以使用 DataBinder.Eval,如本博文所述:

Alternatively, you can create a custom class which inherits BoundField and overrides GetValue to use DataBinder.Eval, as described in this blog post:

http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx