Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将用户定义的数据类型对象绑定到DetailsView?_C#_Asp.net_Data Binding_Object_Detailsview - Fatal编程技术网

C# 如何将用户定义的数据类型对象绑定到DetailsView?

C# 如何将用户定义的数据类型对象绑定到DetailsView?,c#,asp.net,data-binding,object,detailsview,C#,Asp.net,Data Binding,Object,Detailsview,嗨,我有一个包含用户定义的数据类型属性的类。我已经创建了该类的一个实例。当我将该类的对象绑定到DetailsView时,它将显示除用户定义的数据类型属性之外的所有属性。下面是示例代码 public class Customer { public string CustomerName { get; set; } public int Age { get; set; } public Address CustomerAddress { get; set; } } Addr

嗨,我有一个包含用户定义的数据类型属性的类。我已经创建了该类的一个实例。当我将该类的对象绑定到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>
公共类客户
{
公共字符串CustomerName{get;set;}
公共整数{get;set;}
公共地址CustomerAddress{get;set;}
}
Address类看起来像
公共课堂演讲
{
公共字符串Line1{get;set;}
公共字符串第2行{get;set;}
公共字符串City{get;set;}
}
创建Customer类的对象
var cust=新客户{
CustomerName=“abc”,
年龄=25岁,
CustomerAddress=新地址{Line1=“abc”,Line2=“abc”,City=“abc”};
将cust绑定到详细信息视图
List customerInfo=新列表();
客户信息添加(客户);
DetailsView1.DataSource=客户信息;
DetailsView1.DataBind();
In.aspx页面

上述代码未显示客户地址。有人能帮我吗?

在绑定数据时,如果主类(此处的客户)有子类(此处的地址),那么要显示子类对象属性,我们需要使用

因此,请使用以下示例:

<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" />
<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />

在绑定数据时,如果主类(此处的客户)有子类(此处的地址),那么要显示子类对象属性,我们需要使用

因此,请使用以下示例:

<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" />
<asp:BoundField DataField="CustomerAddress.City" HeaderText="City" />

我认为在.NET4中,您可以执行以下操作:


我认为在.NET4中,您可以执行以下操作: