Search code examples
.netasp.netcustom-controlscustom-server-controls

Design Time Attribute For CSS Class in ASP.net Custom Server Control


Hopefully some Custom Control Designers/Builders can help

I'm attempting to build my first custom control that is essential a client detail collection form. There are to be a series of elements to this form that require various styles applied to them. Ideally I'd like the VS 2005/2008 properties interface to be able to apply the CSSClass as it does at the control level, i.e. with a dropdown list of available CSS Clases.

Take for example the Class to be applied to the legend tag

/// <summary>Css Class for Legend</summary>
[Category("Appearance")]
[Browsable(true)]
[DefaultValue("")]
//I am at a loss as to what goes in [Editor]
[Editor(System.Web.UI.CssStyleCollection), typeof(System.Drawing.Design.UITypeEditor))]        
 public string LegendCSSClass
    {
        get { return _LegendCSSClass; }
        set { _LegendCSSClass = value; }
    }

I have tried a couple of options, as you can see from above, without much luck.

Hopefully there is something simple I am missing.

I'd also be happy for references pertaining to the

[Editor]
attribute


Solution

  • Add the CssClassProperty attribute to your property.

    [Category("Appearance")]
    [Browsable(true)]
    [DefaultValue("")]
    [CssClassProperty]
    public string LegendCSSClass    
    {        
       get { return _LegendCSSClass; }        
       set { _LegendCSSClass = value; }    
    }
    

    From MSDN: Adds Cascading Style Sheet (CSS) editing capabilities to a property at design time.

    BTW for this kind of question, Lutz Reflector is your friend. You can look at the attributes that are applied to similar properties in .NET Framework classes.