简体   繁体   中英

ASP.NET Custom WebControl Nested Properties as Object

I am trying to implement a Custom Composite WebControl with "nested" properties, ie, encapsulate a group of properties into a class.

For example, in this composite control, I have placed a button. I would like to be able to encapsulate relevant properties for the button into a class (eg, buttonText, buttonStyle, etc.). This would make defining properties in multi-button/controls composite-control easier and consistent and intuitive.

Note: I would like for the encapsulated properties to appear grouped in the Properties dialog in VisualStudio, in a manner very similar to Style/Font.

Sample:

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
}

Unfortunately, this approach dos not work. The new property myButtonProperties does not appear at all in the "Properies" dialog.

To create a nested property use the System.ComponentModel.DesignerSerializationVisibility attribute in your control like this:

[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public fooButtonProperties myButtonProperties { get { return _profileButtonProp; } }

The final property name will be "myButtonProperties-URL" (with a hyphen). You can also add this attribute to properties in your fooButtonProperties class for even more nesting.
Please note that you may have to close the aspx file and rebuild the solution to refresh the Properties window.

The Category attribute works in your control and in your nested class.

The Description attribute for the descriptions seems correct BUT it does not work which could be a bug in Visual Studio. I found this link: https://www.beta.microsoft.com/VisualStudio/feedback/details/653335/webcontrol-property-descriptions-do-not-appear-in-property-window

Also I observed that no properties show descriptions.

Regards
Oli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM