GridView 绑定了嵌套类的属性 [英] GridView bound with Properties of nested class

查看:22
本文介绍了GridView 绑定了嵌套类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面列出的对象映射.当我尝试在 GridView 中绑定 NestedClass 的属性时,出现错误:

I have an object map similar to what's listed below. When I try to bind the properties of NestedClass in a GridView I get the error:

在所选数据源中找不到名为NestedClass.Name"的字段或属性."

"A field or property with the name 'NestedClass.Name' was not found on the selected data source."

GridView 绑定到一个 ObjectDataSource,而 ObjectDataSource 绑定到一个完全填充的 BoundClass 实例.

The GridView is bound to an ObjectDataSource and the ObjectDataSource is bound to a fully populated instance of BoundClass.

有什么办法可以解决这个问题吗?

Is there any way around this?

示例类:

public class BoundClass
{
    public string Name { get; set; }
    public NestedClass NestedClass { get; set; }
}

public class NestedClass
{
    public string Name { get; set; }
}

推荐答案

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

这篇关于GridView 绑定了嵌套类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆