それはちょうどそれをインスタンス化するのではなく、匿名のオブジェクトにユーザーコントロールをロードすることは可能ですか?
-
26-09-2019 - |
質問
彼らは基本クラスを実装する任意の他のページで使用可能なので、抽象基底クラスで次のように現在、私は、ユーザーコントロールを作成しています:
// 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;
}
何がこのコードについて私に盗聴だが、私はLinqToXMLでTopStrapline
のインスタンスを作成することができるということですが、私はLoadControl
でユーザーコントロールをロードする必要があるため、それは、ユーザーコントロールとしては良いんです。これは動作しますが、それはビット不格好です。理想的には私はLoadControl
匿名オブジェクトに直接pageControls
を実行し、ページのプロパティにそのロードされたコントロールを割り当てることができます。
これは可能ですか?缶誰もが、この問題へのよりよい解決策を提案しますか?おかげ
解決
あなたのためでしょう、この作品?
this.TopStrapline = doc.Elements("TopStrapline")
.Select(e => {
var control = (TopStrapline)LoadControl("~/Path/TropStrapLine.ascx");
control.StraplineText = e.Attribute("text");
return control;
}).FirstOrDefault();
所属していません StackOverflow