2011-08-05 83 views
1

我有一個複雜的類,這是一樣的東西:綁定列表<MyObject>到中繼

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; } } 
    } 

所以,現在,當我綁定的中繼器,像這樣:

rpPeople.DataSource = PeopleNearYou; //this is a List<Person>(); 

,並在實際的中繼,我想展示細節。要訪問,比方說,Pid,所有我需要做的是:

<%# Eval("Pid") %> 

現在,我無法弄清楚如何在中繼

<%# Eval("Fullname") %> //error, fullname not found 

也獲得全名,我想顯示只有首先解決,我不能這樣做,

<%# Eval("Address").First().Universe %> //red, glarring error. can't figure out how 

所以,我將如何顯示這些東西嗎?

非常感謝。

回答

3

如果在綁定中繼器時抓取所需的類成員,這將變得非常容易。

rpPeople.DataSource = PeopleNearYou.Select(r => new 
     { 
      Pid = r.Pid, 
      Universe = r.Addressess.First().Universe, 
      Fullname = r.Name.Fullname 
     } 

現在你需要在你的中繼做的是:

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

看起來不錯,會嘗試 – LocustHorde

0

如果我遇到這樣的複雜情況,我總是使用ItemDataBound事件,因爲您可以獲得更多的控制權。例如,在你情我願創建的項目模板標籤,綁定的ItemDataBound編寫類似這樣的...

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

你需要在e.Item.Type檢查太多,如果你有頁眉/頁腳行。

+0

,但它是一個很大的開銷只是爲了獲得數據不這麼認爲嗎? – LocustHorde