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.