0
目前我生成用戶控件作爲一個抽象基類,因此它們可用於實現基類中的任何其他頁面如下:是否可以將UserControl加載到匿名對象中,而不是僅實例化它?
// doc is an XML file that may or may not contain a TopStrapline node
var pageControls = new {
TopStrapline = (from strap in doc.Elements("TopStrapline")
select new TopStrapline
{
StraplineText =
(string)strap.Attribute("text")
}).FirstOrDefault(),
// loads of other UserControls generated from other nodes
};
// if there's a StrapLine node, I want to make it available as a property
// in this base class. Any page that needs a TopStrapline can just add the base
// class's TopStrapline to a placeholder on the page.
if (pageControls.TopStrapline != null)
{
this.TopStrapline = GetTopStrapline(pageControls.TopStrapline);
}
private TopStrapline GetTopStrapline(TopStrapline strapline)
{
TopStrapline topStrapline = (TopStrapline)LoadControl("~/Path/TopStrapline.ascx");
topStrapline.StraplineText = strapline.StraplineText;
return topStrapline;
}
什麼是竊聽我這個代碼是,我可以創建一個實例TopStrapline
與LinqToXML,但它不是一個好的用戶控件,因爲我需要加載UserControl LoadControl
。這工作,但它似乎有點笨重。理想情況下,我可以將LoadControl
直接執行到pageControls
匿名對象中,然後將該加載的控件分配到頁面的屬性中。
這可能嗎?任何人都可以提出一個更好的解決這個問題?謝謝
是的,這沒關係。謝啦。 – DaveDev 2010-06-10 15:58:53