Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
Data binding 绑定列表<;MyObject>;转发器_Data Binding_C# 4.0_Repeater_Datarepeater - Fatal编程技术网

Data binding 绑定列表<;MyObject>;转发器

Data binding 绑定列表<;MyObject>;转发器,data-binding,c#-4.0,repeater,datarepeater,Data Binding,C# 4.0,Repeater,Datarepeater,我有一门很复杂的课,有点像: public class Person { public int Pid; IList<Address> Addressess; public Name Name; public Name PartnerName; Person(int id) { Addressess = new List<Address>

我有一门很复杂的课,有点像:

    public class Person
    {
        public int Pid;
        IList<Address> Addressess;
        public Name Name;
        public Name PartnerName;

        Person(int id)
        {
            Addressess = new List<Address>();
        }
    }

    public class Address
    {
        public string HouseName;
        public string street;
        public string country;
        public string universe;
        public string galaxy;
    }

    public class Name
    {
        public string Firstname;
        public string Lastname;
        public string Fullname { get { return Firstname + " " + Lastname; } }
    }
现在,我不知道如何在repeater中访问全名

<%# Eval("Fullname") %> //error, fullname not found
//错误,找不到全名
另外,我只想显示第一个地址,但我不能这样做

<%# Eval("Address").First().Universe %> //red, glarring error. can't figure out how
//红色,闪烁错误。不知道怎么做
那么,我该如何展示这些东西呢


非常感谢。

如果遇到这样复杂的情况,我总是使用ItemDataBound事件,因为您可以获得更多的控制。例如,在您的情况下,我会在项目模板中创建一个标签,将ItemDataBound绑定到类似以下的代码

void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((Label)e.Item.FindControl("lblFullName")).Text = ((Person)e.Item.DataItem).FullName;
}

如果有页眉/页脚行,也需要检查e.Item.Type。

如果在绑定转发器时获取所需的类成员,这将非常容易

rpPeople.DataSource = PeopleNearYou.Select(r => new
      {
           Pid = r.Pid,
           Universe = r.Addressess.First().Universe,
           Fullname = r.Name.Fullname
      }
现在,您只需在中继器中执行以下操作:

<%# Eval("Universe") %>
<%# Eval("Fullname") %>

但是仅仅为了获取数据就需要很大的开销,你不这样认为吗?
rpPeople.DataSource = PeopleNearYou.Select(r => new
      {
           Pid = r.Pid,
           Universe = r.Addressess.First().Universe,
           Fullname = r.Name.Fullname
      }
<%# Eval("Universe") %>
<%# Eval("Fullname") %>