2009-10-07 68 views
1

我有一個簡單的UserControl,我想在每個日曆單元格中呈現。我希望它通過傳遞更改UserControl上公開的某些屬性來基於數據集呈現不同的信息。由於某種原因,這是行不通的。我想知道如果頁面循環阻止這個工作正常,因爲它似乎工作,如果我加載控件的PageLoad事件。動態添加UserControl到日曆控件的DayRender事件

例如。

protected void Cal1_DayRender(object sender, DayRenderEventArgs e) 
{ 

    foreach (var item in MyDataSet.Where(s=>s.AvailibilityDate.Date == e.Day.Date)) 
      { 

//the days match. Render a the usercontrol setting some property values to alter the rendering of the control 

MyUserControl userControl = Page.Loadcontrol("~/UserControls/MyUserControl.ascx") as MyUserControl; 
if(userControl != null) 
{ 

//set the properties 

userControl.DisplayText = item.PersonsFullName; 

    e.Cell.Controls.Add(userControl); 
} 

}  

} 

UserControl包含一個名爲DisplayText的屬性,該屬性將值設置爲OnInit方法中文字控件的文本值。

任何想法??

在此先感謝。

回答

0

經過研究發現,DayRender事件在頁面生命週期中發生得太晚,無法添加帶有事件的控件。這就是爲什麼我的init方法沒有觸發並更新文字控件的文本值。

看起來我可能需要編寫一個簡單的類並重寫ToString()方法來寫出必要的HTML。

0

查看我的answer到類似的問題。我找到了一種方式,但它並不漂亮。