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.

5
  • could it be _dynamicProperties is not null? Then when the getter is called, FullName does not get added to it? Commented Feb 3, 2012 at 14:59
  • Can you show your LoadAll method? Commented Feb 3, 2012 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. Commented Feb 3, 2012 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. Commented Feb 3, 2012 at 15:18
  • Did you get a fix or a workaround for this? Commented Apr 18, 2012 at 13:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.