我試圖用「嵌套」屬性實現自定義複合WebControl,即將一組屬性封裝到類中。作爲對象的ASP.NET自定義WebControl嵌套屬性
例如,在這個複合控件中,我放置了一個按鈕。我希望能夠將按鈕的相關屬性封裝到類中(例如,buttonText,buttonStyle等)。這將使得多按鈕/控件組合控件中的定義屬性更加容易且一致且直觀。
注:我想封裝的屬性出現在VisualStudio中的屬性對話框中,以與Style/Font非常相似的方式分組。
樣品:
public class fooButtonProperties
{
[Category("Appearance"), Description("URL for the Profile page")]
public string URL { get; set; }
[Category("Appearance"), Description("Text to display"), DefaultValue("Profile")]
public string ButtonText { get; set; }
/// <summary>
/// Position of the control on the page, default is Right-Aligned
/// </summary>
[Category("Appearance"), Description("Position in the Header"), DefaultValue(PIONEERFramework.Web.UI.WebControls.PageHeaderFooter.Classes.DesignEnum.DesignLayoutEnums.HorizontalPositions.Right)]
///Here is the composite control
public PIONEERFramework.Web.UI.WebControls.PageHeaderFooter.Classes.DesignEnum.DesignLayoutEnums.HorizontalPositions PositionInHeader { get; set; }
}
public class myCustomClass: System.Web.UI.WebControls.CompositeControl
{
protected System.Web.UI.HtmlControls.HtmlLink myButton;
[Category("Appearance")]
public fooButtonProperties myButtonProperties { get { return _profileButtonProp; } }
private fooButtonProperties _myeButtonProp;
#region Constructor
public myCustomClass()
{
this._myeButtonProp = new fooButtonProperties();
}
#endregion
}
不幸的是,這種做法DOS無法正常工作。新屬性myButtonProperties完全不出現在「Properies」對話框中。