使用 ASP.NET 数据绑定的 C# 4.0 动态对象

发布于 2025-01-02 04:25:59 字数 1868 浏览 1 评论 0原文

我试图在 ASP.NET GridView 中显示使用动态对象动态创建的绑定对象的属性。在我的示例中,DynamicProperties.FullName 是动态的。

我的客户端代码是:

<asp:ObjectDataSource runat="server" ID="CustomerDataSource" DataObjectTypeName="Customer" TypeName="CustomerCollection" SelectMethod="LoadAll" />

<asp:GridView ID="CustomerGridView" runat="server" AutoGenerateColumns="False" DataSourceID="CustomerDataSource" EnableViewState="False">
    <Columns>
        <asp:BoundField DataField="FirstName" />
        <asp:BoundField DataField="LastName" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%#Eval("DynamicProperties.FullName")%>' />
            </ItemTemplate>                
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我的 BLL 代码是(为了清楚起见,我对其进行了简化,并且不包括我在 ASP.NET 绑定中使用的 CustomerCollection 声明):

public partial class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private dynamic _dynamicProperties;
        public dynamic DynamicProperties
        {
            get
            {
                if (_dynamicProperties == null)
                {
                    _dynamicProperties = new ExpandoObject();

                    _dynamicProperties.FullName = FirstName + " " + LastName;
                }
                return _dynamicProperties;
            }
        }       
    }

当我运行该应用程序时,出现以下 HttpException 错误: DataBinding:“System.Dynamic.ExpandoObject”不包含名为“FullName”的属性。

我确信我做错了什么,但找不到什么。当我在 Customer 对象中添加名为 FullName 的属性并让 getter 返回 DynamicProperties.FullName 时,它​​就像一个超级按钮(在本例中,我的 ASP.NET Eval 指的是 FullName,而不是 DynamicProperties.FullName) 。

一个想法? 谢谢, 奥米德.

I am trying to display in an ASP.NET GridView a property of a bound object that has been dynamically created using a dynamic object. In my example, DynamicProperties.FullName is dynamic.

My client code is:

<asp:ObjectDataSource runat="server" ID="CustomerDataSource" DataObjectTypeName="Customer" TypeName="CustomerCollection" SelectMethod="LoadAll" />

<asp:GridView ID="CustomerGridView" runat="server" AutoGenerateColumns="False" DataSourceID="CustomerDataSource" EnableViewState="False">
    <Columns>
        <asp:BoundField DataField="FirstName" />
        <asp:BoundField DataField="LastName" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%#Eval("DynamicProperties.FullName")%>' />
            </ItemTemplate>                
        </asp:TemplateField>
    </Columns>
</asp:GridView>

My BLL code is (I simplified it for clarity and did not include the CustomerCollection declaration that I use in my ASP.NET binding):

public partial class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private dynamic _dynamicProperties;
        public dynamic DynamicProperties
        {
            get
            {
                if (_dynamicProperties == null)
                {
                    _dynamicProperties = new ExpandoObject();

                    _dynamicProperties.FullName = FirstName + " " + LastName;
                }
                return _dynamicProperties;
            }
        }       
    }

When I run the application, I got the following HttpException error:
DataBinding: 'System.Dynamic.ExpandoObject' does not contain a property with the name 'FullName'.

I am sure I am doing something wrong but can't find what. When I add a property named FullName in my Customer object and let the getter return DynamicProperties.FullName, it works like a charm (my ASP.NET Eval refers to FullName in this case and not DynamicProperties.FullName).

An idea?
Thanks,
Omid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2025-01-09 04:25:59

Evalobject 作为类型,而您提供dynamic。因此,强制转换将有所帮助,并且使用 Eval 背后的独特属性:

<%# (Container.DataItem as dynamic).FullName%>

或者简称:提供动态对象的地方需要像任何其他类型一样对待,因为它与对象不同。

Eval takes object as type, while you provide dynamic. So a cast will help and the use of the distinct property behind Eval:

<%# (Container.DataItem as dynamic).FullName%>

Or short: where object is provided dynamic needs to treated like any other type as it is different from object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文