WebControl ItemTemplate - Event Kalender

  • VB.NET

    WebControl ItemTemplate - Event Kalender

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Web.UI
    2. Imports System.Web.UI.WebControls
    3. Imports System.Data
    4. Imports System.ComponentModel
    5. Namespace DataControls
    6. '*******************************************************
    7. ' Class DataCalendarItem
    8. ' - serves as the container for a single calendar entry,
    9. ' allowing for databinding syntax like the following
    10. ' to be used in the .aspx page:
    11. '
    12. ' <%# Container.DataItem("MyField") %>
    13. ' *******************************************************
    14. Public Class DataCalendarItem
    15. Inherits Control
    16. Implements INamingContainer
    17. Private _dataItem As DataRow
    18. Public Sub New(ByVal dr As DataRow)
    19. _dataItem = dr
    20. End Sub
    21. ' because the source data will be a DataTable
    22. ' object, it makes sense for our DataItem
    23. ' property to return a DataRow object
    24. ' (i.e. a single item in the data source
    25. ' corresponds to a single row of data)
    26. Public Property DataItem() As DataRow
    27. Get
    28. Return _dataItem
    29. End Get
    30. Set(ByVal value As DataRow)
    31. _dataItem = value
    32. End Set
    33. End Property
    34. End Class
    35. '*******************************************************
    36. ' Class DataCalendar
    37. ' - subclass of the ASP.NET Calendar control for
    38. ' displaying events from a DataTable with support
    39. ' for templates
    40. ' *******************************************************
    41. Public Class DataCalendar
    42. Inherits Calendar
    43. Implements INamingContainer
    44. Private _dataSource As Object
    45. Private _dataMember As String
    46. Private _dayField As String
    47. Private _itemTemplate As ITemplate
    48. Private _noEventsTemplate As ITemplate
    49. Private _dayWithEventsStyle As TableItemStyle
    50. Private _dtSource As DataTable
    51. ' Support either a DataSet or DataTable object
    52. ' for the DataSource property
    53. Public Property DataSource() As Object
    54. Get
    55. Return _dataSource
    56. End Get
    57. Set(ByVal value As Object)
    58. If TypeOf value Is DataTable OrElse TypeOf value Is DataSet Then
    59. _dataSource = value
    60. Else
    61. Throw New Exception("The DataSource property of the DataCalendar control" & " must be a DataTable or DataSet object")
    62. End If
    63. End Set
    64. End Property
    65. ' If a DataSet is supplied for DataSource,
    66. ' use this property to determine which
    67. ' DataTable within the DataSet should
    68. ' be used; if DataMember is not supplied,
    69. ' the first table in the DataSet will
    70. ' be used.
    71. Public Property DataMember() As String
    72. Get
    73. Return _dataMember
    74. End Get
    75. Set(ByVal value As String)
    76. _dataMember = value
    77. End Set
    78. End Property
    79. ' Specify the name of the field within
    80. ' the source DataTable that contains
    81. ' a DateTime value for displaying in the
    82. ' calendar.
    83. Public Property DayField() As String
    84. Get
    85. Return _dayField
    86. End Get
    87. Set(ByVal value As String)
    88. _dayField = value
    89. End Set
    90. End Property
    91. Public Property DayWithEventsStyle() As TableItemStyle
    92. Get
    93. Return _dayWithEventsStyle
    94. End Get
    95. Set(ByVal value As TableItemStyle)
    96. _dayWithEventsStyle = value
    97. End Set
    98. End Property
    99. <TemplateContainer(GetType(DataCalendarItem))> _
    100. Public Property ItemTemplate() As ITemplate
    101. Get
    102. Return _itemTemplate
    103. End Get
    104. Set(ByVal value As ITemplate)
    105. _itemTemplate = value
    106. End Set
    107. End Property
    108. <TemplateContainer(GetType(DataCalendarItem))> _
    109. Public Property NoEventsTemplate() As ITemplate
    110. Get
    111. Return _noEventsTemplate
    112. End Get
    113. Set(ByVal value As ITemplate)
    114. _noEventsTemplate = value
    115. End Set
    116. End Property
    117. ' Constructor
    118. Public Sub New()
    119. MyBase.New()
    120. ' since this control will be used for displaying
    121. ' events, set these properties as a default
    122. Me.SelectionMode = CalendarSelectionMode.None
    123. Me.ShowGridLines = True
    124. End Sub
    125. Private Sub SetupCalendarItem(ByVal cell As TableCell, ByVal r As DataRow, ByVal t As ITemplate)
    126. ' given a calendar cell and a datarow, set up the
    127. ' templated item and resolve data binding syntax
    128. ' in the template
    129. Dim dti As New DataCalendarItem(r)
    130. t.InstantiateIn(dti)
    131. dti.DataBind()
    132. cell.Controls.Add(dti)
    133. End Sub
    134. Protected Overrides Sub OnDayRender(ByVal cell As TableCell, ByVal day As CalendarDay)
    135. ' _dtSource was already set by the Render method
    136. If _dtSource IsNot Nothing Then
    137. ' We have the data source as a DataTable now;
    138. ' filter the records in the DataTable for the given day;
    139. ' force the date format to be MM/dd/yyyy
    140. ' to ensure compatibility with RowFilter
    141. ' date expression syntax (#date#).
    142. ' Also, take the possibility of time
    143. ' values into account by specifying
    144. ' a date range, to include the full day
    145. Dim dv As New DataView(_dtSource)
    146. dv.RowFilter = String.Format("{0} >= #{1}# and {0} < #{2}#", Me.DayField, day.[Date].ToString("MM/dd/yyyy"), day.[Date].AddDays(1).ToString("MM/dd/yyyy"))
    147. ' are there events on this day?
    148. If dv.Count > 0 Then
    149. ' there are events on this day; if indicated,
    150. ' apply the DayWithEventsStyle to the table cell
    151. If Me.DayWithEventsStyle IsNot Nothing Then
    152. cell.ApplyStyle(Me.DayWithEventsStyle)
    153. End If
    154. ' for each event on this day apply the
    155. ' ItemTemplate, with data bound to the item's row
    156. ' from the data source
    157. If Me.ItemTemplate IsNot Nothing Then
    158. For i As Integer = 0 To dv.Count - 1
    159. SetupCalendarItem(cell, dv(i).Row, Me.ItemTemplate)
    160. Next
    161. End If
    162. Else
    163. ' no events this day;
    164. If Me.NoEventsTemplate IsNot Nothing Then
    165. SetupCalendarItem(cell, Nothing, Me.NoEventsTemplate)
    166. End If
    167. End If
    168. End If
    169. ' call the base render method too
    170. MyBase.OnDayRender(cell, day)
    171. End Sub
    172. Protected Overrides Sub Render(ByVal html As HtmlTextWriter)
    173. _dtSource = Nothing
    174. If Me.DataSource IsNot Nothing AndAlso Me.DayField IsNot Nothing Then
    175. ' determine if the datasource is a DataSet or DataTable
    176. If TypeOf Me.DataSource Is DataTable Then
    177. _dtSource = DirectCast(Me.DataSource, DataTable)
    178. End If
    179. If TypeOf Me.DataSource Is DataSet Then
    180. Dim ds As DataSet = DirectCast(Me.DataSource, DataSet)
    181. If Me.DataMember Is Nothing OrElse Me.DataMember = "" Then
    182. ' if data member isn't supplied, default to the first table
    183. _dtSource = ds.Tables(0)
    184. Else
    185. ' if data member is supplied, use it
    186. _dtSource = ds.Tables(Me.DataMember)
    187. End If
    188. End If
    189. ' throw an exception if there is a problem with the data source
    190. If _dtSource Is Nothing Then
    191. Throw New Exception("Error finding the DataSource. Please check " & " the DataSource and DataMember properties.")
    192. End If
    193. End If
    194. ' call the base Calendar's Render method
    195. ' allowing OnDayRender() to be executed
    196. MyBase.Render(html)
    197. End Sub
    198. End Class


    Wie so kann ich bei dem Control kein <ItemTemplate> </ItemTemplate> setzten? Habe mir auf mehreren Seiten angeschaut wie das funktionieren soll und auch nach deren beispiel nachgebaut, jedoch funktioniert es leider nicht. Bitte helft mir :)