1

そのような3つのカスタムユーザーコントロールを定義しました:

public partial abstract class MyAbstractControl : Usercontrol{
  // Base class definition here, with common property and methods
  public string CommonAttribute {get; set;}
}

public partial class MyConcreteControl1 : MyAbstractControl{
  // Some specific stuff here
}

public partial class MyConcreteControl2 : MyAbstractControl{
  // Other specific but different stuff here
}

次に、基本クラスのプロパティを持つ別の UserControl を定義しました。

public partial class MyBeautifulControl : UserControl{
  [PersistenceMode(PersistenceMode.InnerProperty)]
  public MyAbstractControl ChildElement{get;set;}
}

aspx ファイルでは、このコントロールを使用していますが、 MyAbstractControlの代わりにMyConcreteControl1のインスタンスを定義したいと考えています。

しかし、私が書くと:

<MyBeautifulControl runat="server" id="beautiful">
  <ChildElement commonAttribute="value" />
</MyBeautifulControl>

ChildElementはMyAbstractControlインスタンスとしてのみ定義できます。コンテキストに応じてMyConcreteControl1またはMyConcreteControl2インスタンスを作成したいのですが、方法がわかりません。

4

1 に答える 1

0

次を使用しSystem.Web.UI.TemplateContainerAttributeます。

[PersistenceMode(PersistenceMode.InnerProperty), 
 TemplateContainer(typeof(MyConcreteControl))]
public MyAbstractControl ChildElement{get;set;}

詳細については、ドキュメントを参照してください。についてのHOWTOもあります。

于 2010-12-10T09:55:45.923 に答える