开发者

How to bind user defined data type object to DetailsView?

Hi I am having a class which contains user defined data type property. I have created an instance of that class. When I bind that object of that class to DetailsView it is showing all properties except user defined data type property. Here is the sample code.

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>开发者_StackOverflow

Above code is not displaying Customer Address. Can any one help me ?


While binding data , in case the Main Class ( Customer here ) has Child class ( Address here ), then to display the Child class object properties, we need to use <asp:TemplateField /> .

So, use the below sample:

<asp:TemplateField HeaderText=”City”>
    <ItemTemplate>
        <asp:Label ID=”customerCity” runat=”server” 
                   Text='<%# Eval("CustomerAddress.City") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Below code was Expected to work BUT it will NOT :

<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />


I think in .NET4 you can do:

<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜