简体   繁体   English

ASP.NET自定义WebControl嵌套属性作为对象

[英]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. 我正在尝试实现具有“嵌套”属性的自定义复合Web控件,即将一组属性封装到一个类中。

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.). 我希望能够将按钮的相关属性封装到一个类中(例如,buttonText,buttonStyle等)。 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. 注意:我希望封装的属性以与样式/字体非常相似的方式分组显示在VisualStudio的“属性”对话框中。

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. 新属性myButtonProperties根本不会出现在“属性”对话框中。

To create a nested property use the System.ComponentModel.DesignerSerializationVisibility attribute in your control like this: 要创建嵌套属性,请使用控件中的System.ComponentModel.DesignerSerializationVisibility属性,如下所示:

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

The final property name will be "myButtonProperties-URL" (with a hyphen). 最终的属性名称将为“ myButtonProperties-URL”(带连字符)。 You can also add this attribute to properties in your fooButtonProperties class for even more nesting. 您也可以将此属性添加到fooButtonProperties类的属性中,以进行更多的嵌套。
Please note that you may have to close the aspx file and rebuild the solution to refresh the Properties window. 请注意,您可能必须关闭aspx文件并重建解决方案以刷新“属性”窗口。

The Category attribute works in your control and in your nested class. Category属性可在您的控件和嵌套类中使用。

The Description attribute for the descriptions seems correct BUT it does not work which could be a bug in Visual Studio. Description属性似乎是正确的,但是它不起作用,这可能是Visual Studio中的错误。 I found this link: https://www.beta.microsoft.com/VisualStudio/feedback/details/653335/webcontrol-property-descriptions-do-not-appear-in-property-window 我找到了此链接: 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 奥利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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