使用 ASP.NET 数据绑定的 C# 4.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Eval 将
object
作为类型,而您提供dynamic
。因此,强制转换将有所帮助,并且使用 Eval 背后的独特属性:或者简称:提供动态对象的地方需要像任何其他类型一样对待,因为它与对象不同。
Eval takes
object
as type, while you providedynamic
. So a cast will help and the use of the distinct property behind Eval:Or short: where object is provided dynamic needs to treated like any other type as it is different from object.