2009-07-15 72 views
0

我有這樣的代碼隱藏,當它是數據綁定,以查看作者/日期是否爲空(無論作者/日期,或者系統設置爲不顯示它們)時檢查中繼器中的每個項目,這樣我就可以清除它們各自的標籤。這是因爲當沒有指定作者和/或日期時,我沒有得到像「發佈者」這樣的內容。是否可以從數據綁定項中檢索屬性名稱?

下面是代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    repeater.ItemDataBound += new RepeaterItemEventHandler(repeater_ItemDataBound);  
} 

void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     Literal PostedBy = (Literal)e.Item.FindControl("litPostedBy"); 
     Literal PostedOn = (Literal)e.Item.FindControl("litPostedOn"); 
     string Author = (string)DataBinder.Eval(e.Item.DataItem, "Author"); 
     string Date = (string)DataBinder.Eval(e.Item.DataItem, "PubDate"); 

     if (string.IsNullOrEmpty(Author)) 
     { 
      if (string.IsNullOrEmpty(Date)) 
      { 
       PostedBy.Text = ""; 
       PostedOn.Text = ""; 
      } 
      else 
      { 
       PostedBy.Text = "Posted "; 
      } 

     } 
    } 
} 

我使用的是CMS,而且我不確定什麼所有屬性都在e.Item.DataItem。有什麼方法可以循環訪問DataItem並打印出屬性名稱/值?

謝謝!

回答

2

DataItem的屬性取決於它包含的對象類型。它將包含數據綁定中繼器時當前正在處理的數據源中的對象。以下方法需要的任何對象,並將其列包含屬性:

private static void PrintAllProperties(object obj) 
{ 
    obj.GetType(). 
     GetProperties(). 
     ToList(). 
     ForEach(p => 
      Console.WriteLine("{0} [{1}]", p.Name, p.PropertyType.ToString() 
      )); 
} 

實施例輸出(用於String實例):

Chars [System.Char] 
Length [System.Int32] 
+0

確定這就是良好的信息,除我沒有看到「作者」在結果。有什麼我可以做的嗎? – Anders 2009-07-15 13:58:17

相關問題