4

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.

Omid B.
  • 356
  • 4
  • 13
  • could it be _dynamicProperties is not null? Then when the getter is called, FullName does not get added to it? – Mike Ohlsen Feb 03 '12 at 14:59
  • Can you show your LoadAll method? – Jeremy Armstrong Feb 03 '12 at 14:59
  • @Jeremy : the LoadAll method has been generated by a tool (CodeFluent Entities) so it should works. When I put traces (or even VS debugging), the method is triggered and data are loaded correctly. If I remove the ItemTemplate that refers to my dynamic object, the data is displayed without any problem in the GridView. – Omid B. Feb 03 '12 at 15:14
  • 1
    @Mike: when I perform a step by step debugging through VS, I go through the the DynamicProperties property is called and the Title dynamic property is added without any problem. When I press F10 to continue the processing and the step reached the **#Eval("DynamicProperties.FullName")**, it raises the described exception. It's like the DataBinding is breaking the Dynamic behavior. – Omid B. Feb 03 '12 at 15:18
  • Did you get a fix or a workaround for this? – Burt Apr 18 '12 at 13:36

1 Answers1

4

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.

Joerg Krause
  • 1,759
  • 1
  • 16
  • 27