您好我有一个包含用户定义的数据类型属性的类。我已经创建了该类的实例。当我将该类的对象绑定到DetailsView时,它显示除用户定义的数据类型属性之外的所有属性。这是示例代码。
public class Customer
{
public string CustomerName { get; set; }
public int Age { get; set; }
public Address CustomerAddress { get; set; }
}
Address class looks like
public class Address
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
}
Creating an object of Customer class
var cust = new Customer {
CustomerName = "abc",
Age = 25,
CustomerAddress = new Address{ Line1 = "abc", Line2 = "abc", City = "abc" }};
Binding cust to Details View
List<Customer> customerInfo = new List<Customer>();
customerInfo.Add(cust);
DetailsView1.DataSource = custmerInfo;
DetailsView1.DataBind();
In .aspx page
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False">
<Fields>
<asp:BoundField DataField="CustomerName" HeaderText="Customer Name">
<asp:BoundField DataField="Age" HeaderText="Customer Age">
<asp:BoundField DataField="CustomerAddress" HeaderText="Customer Address ">
</Fields>
</asp:DetailsView>
以上代码未显示客户地址。任何人都可以帮助我吗?
答案 0 :(得分:0)
在绑定数据时,如果Main Class(Customer here)具有Child类(此处为Address),则要显示Child类对象属性,我们需要使用<asp:TemplateField />
。
因此,请使用以下示例:
<asp:TemplateField HeaderText=”City”>
<ItemTemplate>
<asp:Label ID=”customerCity” runat=”server”
Text='<%# Eval("CustomerAddress.City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
下面的代码预计会工作,但不会:
<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />
答案 1 :(得分:0)
我认为在.NET4中你可以做到:
<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />