【问题标题】:ASP.NET Custom WebControl Nested Properties as ObjectASP.NET 自定义 WebControl 嵌套属性作为对象
【发布时间】:2015-03-18 20:51:32
【问题描述】:

我正在尝试使用“嵌套”属性实现自定义复合 WebControl,即将一组属性封装到一个类中。

例如,在这个复合控件中,我放置了一个按钮。我希望能够将按钮的相关属性封装到一个类中(例如,buttonText、buttonStyle 等)。这将使在多按钮/控件复合控件中定义属性更容易、一致和直观。

注意:我希望封装的属性以与样式/字体非常相似的方式分组显示在 VisualStudio 的“属性”对话框中。

示例:

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
}

不幸的是,这种方法不起作用。新属性 myButtonProperties 根本不会出现在“属性”对话框中。

【问题讨论】:

    标签: c# asp.net web-controls


    【解决方案1】:

    要创建嵌套属性,请使用控件中的System.ComponentModel.DesignerSerializationVisibility 属性,如下所示:

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

    最终的属性名称将是“myButtonProperties-URL”(带有连字符)。您还可以将此属性添加到 fooButtonProperties 类中的属性中,以实现更多嵌套。
    请注意,您可能必须关闭 aspx 文件并重建解决方案以刷新“属性”窗口。

    Category 属性适用于您的控件和嵌套类。

    描述的Description 属性似乎是正确的,但它不起作用,这可能是 Visual Studio 中的一个错误。我找到了这个链接: https://www.beta.microsoft.com/VisualStudio/feedback/details/653335/webcontrol-property-descriptions-do-not-appear-in-property-window

    我还观察到没有属性显示描述。

    问候
    奥利

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      相关资源
      最近更新 更多