VB.NET-Quellcode
- Imports System.Web.UI
- Imports System.Web.UI.WebControls
- Imports System.Data
- Imports System.ComponentModel
- Namespace DataControls
- '*******************************************************
- ' Class DataCalendarItem
- ' - serves as the container for a single calendar entry,
- ' allowing for databinding syntax like the following
- ' to be used in the .aspx page:
- '
- ' <%# Container.DataItem("MyField") %>
- ' *******************************************************
- Public Class DataCalendarItem
- Inherits Control
- Implements INamingContainer
- Private _dataItem As DataRow
- Public Sub New(ByVal dr As DataRow)
- _dataItem = dr
- End Sub
- ' because the source data will be a DataTable
- ' object, it makes sense for our DataItem
- ' property to return a DataRow object
- ' (i.e. a single item in the data source
- ' corresponds to a single row of data)
- Public Property DataItem() As DataRow
- Get
- Return _dataItem
- End Get
- Set(ByVal value As DataRow)
- _dataItem = value
- End Set
- End Property
- End Class
- '*******************************************************
- ' Class DataCalendar
- ' - subclass of the ASP.NET Calendar control for
- ' displaying events from a DataTable with support
- ' for templates
- ' *******************************************************
- Public Class DataCalendar
- Inherits Calendar
- Implements INamingContainer
- Private _dataSource As Object
- Private _dataMember As String
- Private _dayField As String
- Private _itemTemplate As ITemplate
- Private _noEventsTemplate As ITemplate
- Private _dayWithEventsStyle As TableItemStyle
- Private _dtSource As DataTable
- ' Support either a DataSet or DataTable object
- ' for the DataSource property
- Public Property DataSource() As Object
- Get
- Return _dataSource
- End Get
- Set(ByVal value As Object)
- If TypeOf value Is DataTable OrElse TypeOf value Is DataSet Then
- _dataSource = value
- Else
- Throw New Exception("The DataSource property of the DataCalendar control" & " must be a DataTable or DataSet object")
- End If
- End Set
- End Property
- ' If a DataSet is supplied for DataSource,
- ' use this property to determine which
- ' DataTable within the DataSet should
- ' be used; if DataMember is not supplied,
- ' the first table in the DataSet will
- ' be used.
- Public Property DataMember() As String
- Get
- Return _dataMember
- End Get
- Set(ByVal value As String)
- _dataMember = value
- End Set
- End Property
- ' Specify the name of the field within
- ' the source DataTable that contains
- ' a DateTime value for displaying in the
- ' calendar.
- Public Property DayField() As String
- Get
- Return _dayField
- End Get
- Set(ByVal value As String)
- _dayField = value
- End Set
- End Property
- Public Property DayWithEventsStyle() As TableItemStyle
- Get
- Return _dayWithEventsStyle
- End Get
- Set(ByVal value As TableItemStyle)
- _dayWithEventsStyle = value
- End Set
- End Property
- <TemplateContainer(GetType(DataCalendarItem))> _
- Public Property ItemTemplate() As ITemplate
- Get
- Return _itemTemplate
- End Get
- Set(ByVal value As ITemplate)
- _itemTemplate = value
- End Set
- End Property
- <TemplateContainer(GetType(DataCalendarItem))> _
- Public Property NoEventsTemplate() As ITemplate
- Get
- Return _noEventsTemplate
- End Get
- Set(ByVal value As ITemplate)
- _noEventsTemplate = value
- End Set
- End Property
- ' Constructor
- Public Sub New()
- MyBase.New()
- ' since this control will be used for displaying
- ' events, set these properties as a default
- Me.SelectionMode = CalendarSelectionMode.None
- Me.ShowGridLines = True
- End Sub
- Private Sub SetupCalendarItem(ByVal cell As TableCell, ByVal r As DataRow, ByVal t As ITemplate)
- ' given a calendar cell and a datarow, set up the
- ' templated item and resolve data binding syntax
- ' in the template
- Dim dti As New DataCalendarItem(r)
- t.InstantiateIn(dti)
- dti.DataBind()
- cell.Controls.Add(dti)
- End Sub
- Protected Overrides Sub OnDayRender(ByVal cell As TableCell, ByVal day As CalendarDay)
- ' _dtSource was already set by the Render method
- If _dtSource IsNot Nothing Then
- ' We have the data source as a DataTable now;
- ' filter the records in the DataTable for the given day;
- ' force the date format to be MM/dd/yyyy
- ' to ensure compatibility with RowFilter
- ' date expression syntax (#date#).
- ' Also, take the possibility of time
- ' values into account by specifying
- ' a date range, to include the full day
- Dim dv As New DataView(_dtSource)
- 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"))
- ' are there events on this day?
- If dv.Count > 0 Then
- ' there are events on this day; if indicated,
- ' apply the DayWithEventsStyle to the table cell
- If Me.DayWithEventsStyle IsNot Nothing Then
- cell.ApplyStyle(Me.DayWithEventsStyle)
- End If
- ' for each event on this day apply the
- ' ItemTemplate, with data bound to the item's row
- ' from the data source
- If Me.ItemTemplate IsNot Nothing Then
- For i As Integer = 0 To dv.Count - 1
- SetupCalendarItem(cell, dv(i).Row, Me.ItemTemplate)
- Next
- End If
- Else
- ' no events this day;
- If Me.NoEventsTemplate IsNot Nothing Then
- SetupCalendarItem(cell, Nothing, Me.NoEventsTemplate)
- End If
- End If
- End If
- ' call the base render method too
- MyBase.OnDayRender(cell, day)
- End Sub
- Protected Overrides Sub Render(ByVal html As HtmlTextWriter)
- _dtSource = Nothing
- If Me.DataSource IsNot Nothing AndAlso Me.DayField IsNot Nothing Then
- ' determine if the datasource is a DataSet or DataTable
- If TypeOf Me.DataSource Is DataTable Then
- _dtSource = DirectCast(Me.DataSource, DataTable)
- End If
- If TypeOf Me.DataSource Is DataSet Then
- Dim ds As DataSet = DirectCast(Me.DataSource, DataSet)
- If Me.DataMember Is Nothing OrElse Me.DataMember = "" Then
- ' if data member isn't supplied, default to the first table
- _dtSource = ds.Tables(0)
- Else
- ' if data member is supplied, use it
- _dtSource = ds.Tables(Me.DataMember)
- End If
- End If
- ' throw an exception if there is a problem with the data source
- If _dtSource Is Nothing Then
- Throw New Exception("Error finding the DataSource. Please check " & " the DataSource and DataMember properties.")
- End If
- End If
- ' call the base Calendar's Render method
- ' allowing OnDayRender() to be executed
- MyBase.Render(html)
- End Sub
- 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