locked
Hyphens in property names- automatically generated properties? RRS feed

  • Question

  • User-916962509 posted

    Hi everybody.

    I've been writing some custom controls recently, and I'm flummoxed by something which I have seen in use in the framework. Take the example of the asp:Calendar control. Markup like this in a .aspx page would be perfectly valid:

     <asp:Calendar ID="cal1" runat="server" OtherMonthDayStyle-CssClass="myOtherMonthStyle" />

    What I would like to know is how properties such as "OtherMonthDayStyle-CssClass" are exposed. What this property appears to access to is "OtherMonthDayStyle" which is a TableItemStyle object's CssClass property (string).

    There are also many similar property names, "OtherMonthDayStyle-[someproperty]" which can be set in the .aspx page when marking up the calendar control.

    Interestingly when I try to create a property in a control, the compiler will not allow me to use hyphens in the property names.

    I'd like to know where these hyphenated properties are defined within the calendar control, and how the framework knows what I want to set when, in markup, I type "OtherMonthDayStyle-CssClass="myclass""

    I've had a look in Reflector at the Calendar class- the properties are not explicitely defined. I therefore believe that the properties are automatically generated (or linked somehow) - but how? Is there some code running which exposes a property on the calendar for each property of the "OtherMonthDayStyle" object? If so, where is it defined?

    If anybody can offer some insight into this, I'd appreciate it. I've found workarounds, but they're not elegant, and I'd like to know how these properties work to satisfy my curiosity... Many thanks!

    Tuesday, July 29, 2008 3:59 AM

Answers

All replies

  • User-1776072412 posted

    See the very end of this page http://msdn.microsoft.com/en-us/library/aa720440(VS.71).aspx - it's a special syntax for setting sub-properties of complex types.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 29, 2008 4:51 AM
  • User-916962509 posted

    Thanks.

    With the aid of that article as a starting point, I've sussed it.

    Cheers for your help,

    Ben.

    PS: Keep up the good work at red-gate.

    Tuesday, July 29, 2008 7:42 AM
  • User1943548895 posted

    <p></p>

    Using the example to add a composite control that includes a calendar (below I only include the calendar control), the steps to follow are along the lines of:
    . Add a property defined as TableStyleItem for each of the calendar style properties you want
    . if you want the designer to recognise the property and intellisense-it for you you need the <> instructions (see below)
    . add each TableStyleItem property to the calendar instance on the prerender



         Public Class CalendarD
            Inherits CompositeControl               '(needs to inherit from compositecontrol or webcontrol)
            Implements INamingContainer

             Public oCalendar As Calendar        'this will work as your calendar  which will get displayed

           'here add all variables to contain the calendar sub properties
            Private _calendarCSSClass As String = Nothing
            Private _calendarDayHeaderStyle As TableItemStyle = Nothing
            Private _calendarDayStyle As TableItemStyle = Nothing
            Private _calendarNextPrevStyle As TableItemStyle = Nothing
            Private _calendarOtherMonthDayStyle As TableItemStyle = Nothing
            Private _calendarSelectedDayStyle As TableItemStyle = Nothing
            Private _calendarSelectorStyle As TableItemStyle = Nothing
            Private _calendarTitleStyle As TableItemStyle = Nothing
            Private _calendarTodayDayStyle As TableItemStyle = Nothing
            Private _calendarWeekendDayStyle As TableItemStyle = Nothing


            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarDayHeaderStyle() As TableItemStyle
                Get
                    If Me._calendarDayHeaderStyle Is Nothing Then
                        Me._calendarDayHeaderStyle = New TableItemStyle
                    End If
                    Return Me._calendarDayHeaderStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarDayStyle() As TableItemStyle
                Get
                    If Me._calendarDayStyle Is Nothing Then
                        Me._calendarDayStyle = New TableItemStyle
                    End If
                    Return Me._calendarDayStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarNextPrevStyle() As TableItemStyle
                Get
                    If Me._calendarNextPrevStyle Is Nothing Then
                        Me._calendarNextPrevStyle = New TableItemStyle
                    End If
                    Return Me._calendarNextPrevStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarOtherMonthDayStyle() As TableItemStyle
                Get
                    If Me._calendarOtherMonthDayStyle Is Nothing Then
                        Me._calendarOtherMonthDayStyle = New TableItemStyle
                    End If
                    Return Me._calendarOtherMonthDayStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarSelectedDayStyle() As TableItemStyle
                Get
                    If Me._calendarSelectedDayStyle Is Nothing Then
                        Me._calendarSelectedDayStyle = New TableItemStyle
                    End If
                    Return Me._calendarSelectedDayStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarSelectorStyle() As TableItemStyle
                Get
                    If Me._calendarSelectorStyle Is Nothing Then
                        Me._calendarSelectorStyle = New TableItemStyle
                    End If
                    Return Me._calendarSelectorStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarTitleStyle() As TableItemStyle
                Get
                    If Me._calendarTitleStyle Is Nothing Then
                        Me._calendarTitleStyle = New TableItemStyle
                    End If
                    Return Me._calendarDayStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarTodayDayStyle() As TableItemStyle
                Get
                    If Me._calendarTodayDayStyle Is Nothing Then
                        Me._calendarTodayDayStyle = New TableItemStyle
                    End If
                    Return Me._calendarTodayDayStyle
                End Get
            End Property

            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True)> _
            Public ReadOnly Property CalendarWeekendDayStyle() As TableItemStyle
                Get
                    If Me._calendarWeekendDayStyle Is Nothing Then
                        Me._calendarWeekendDayStyle = New TableItemStyle
                    End If
                    Return Me._calendarWeekendDayStyle
                End Get
            End Property

            Public Sub New()
                MyBase.New()

                oCalendar = New Calendar

            End Sub

            Protected Overrides Sub CreateChildControls()
                Me.Controls.Add(oCalendar)
                MyBase.CreateChildControls()
            End Sub

             Protected Overrides Sub RenderChildren(ByVal myHTWriter As System.Web.UI.HtmlTextWriter)

                'continue to render rest of calendar
                oCalendar.RenderControl(myHTWriter)

            End Sub

            'here adds the TableStyleItem properties to the Calendar control on the prerender
            Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)

                MyBase.OnPreRender(e)

                'add the properties to the Calendar Render
                oCalendar.DayHeaderStyle.MergeWith(_calendarDayHeaderStyle)
                oCalendar.DayHeaderStyle.MergeWith(_calendarDayHeaderStyle)
                oCalendar.DayStyle.MergeWith(_calendarDayStyle)
                oCalendar.NextPrevStyle.MergeWith(_calendarNextPrevStyle)
                oCalendar.OtherMonthDayStyle.MergeWith(_calendarOtherMonthDayStyle)
                oCalendar.SelectedDayStyle.MergeWith(_calendarSelectedDayStyle)
                oCalendar.SelectorStyle.MergeWith(_calendarSelectorStyle)
                oCalendar.TitleStyle.MergeWith(_calendarTitleStyle)
                oCalendar.TodayDayStyle.MergeWith(_calendarTodayDayStyle)
                oCalendar.WeekendDayStyle.MergeWith(_calendarWeekendDayStyle)

            End Sub

     

    End Class

     

     

    Tuesday, July 29, 2008 9:10 AM
  • User1943548895 posted

    Then on the designed to use the new class: 
    (considering we've defined the CalendarD class within a namespace with a shortcut of 'SCC' - defined in the web.config)
    (also note we defined CSS for each properties so all CSS definitions are on a separate cascading stylesheets file, and used the name of the calendar property for the CSS name)

     

    <SCC:CalendarD ID="Calendar1" runat="server"
                                     UseAccessibleHeader="true"
                                     ShowTitle="false"
                                     FirstDayOfWeek="Monday"
                                     NextPrevFormat="FullMonth"

                                     CalendarDayStyle-CssClass = "DayStyle-CssClass"
                                     CalendarWeekendDayStyle-CssClass = "WeekendDayStyle-CssClass"
                                     CalendarOtherMonthDayStyle-CssClass="OtherMonthDayStyle-CssClass"
                                     CalendarSelectedDayStyle-CssClass="SelectedDayStyle-CssClass"
                                     CalendarTodayDayStyle-CssClass="TodayDayStyle-CssClass"
                                     CalendarSelectorStyle-CssClass="SelectorStyle-CssClass"
                                     CalendarNextPrevStyle-CssClass="NextPrevStyle-CssClass"
                                     CalendarDayHeaderStyle-CssClass="DayHeaderStyle-CssClass"
                                     CalendarTitleStyle-CssClass="TitleStyle-CssClass"
                                 >
                             </SCC:CalendarD>

    Tuesday, July 29, 2008 9:25 AM