2010-12-13 57 views
2

嗨我有一個類,其中包含用戶定義的數據類型屬性。我創建了該類的一個實例。當我將該類的該對象綁定到DetailsView時,它將顯示除用戶定義的數據類型屬性以外的所有屬性。這裏是示例代碼。如何將用戶定義的數據類型對象綁定到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

綁定數據時,如果Main Class(Customer here)有Child class(Address here),然後顯示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" /> 
0

我想在.NET4你可以這樣做:

<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" /> 
相關問題