| |||||
Customizing Individual DaysControls You Can Use on Web Forms ASP.NET Standard Controls Calendar Control By default, days in the Calendar control are simply displayed as numbers. ( If day selection is enabled, the numbers appear as links. For details, see Controlling User Date Selection. ) However, you can customize the appearance and content of individual days, which allows you to:
When the Calendar control is creating the output to send to the browser, it raises an DayRender event you can handle. The control calls your method for each day as it is preparing the day for display, and you can then programmatically examine which date is being rendered and customize it appropriately. The DayRender event method takes two arguments, a reference to the control raising the event ( the Calendar control ) and an object of type DayRenderEvent. The DayRenderEvent object provides access to two additional objects:
To customize the appearance of an individual day
Public Sub Calendar1_DayRender ( ByValControls As Object, _ ByVal e As DayRenderEvent )
Set e.Cell.BackColor = Colors.Red The following example shows a simple but complete method that illustrates how to change the appearance of individual days. The method causes every other day in the calender to be rendered in green; alternating days are rendered in the default day color. public Sub Calendar1_DayRender ( ByVal sender as Object, _
ByVal e as DayRenderEventArgs )
With e
If Not .Day.IsOtherMonth And .Day.DayNumberText Mod 2 = 0 Then
' For even-numbered days in the current month,
' set the background color of the cell to green.
.Cell.BackColor = System.Drawing.Color.Parse ( "Green" )
EndIf
End With
End Sub
To specify that an individual day can be selected
public Sub Calendar1_DayRender ( ByVal sender as Object, _
ByVal e as DayRenderEventArgs )
myHoliday = new DateTime ( 2000, 7, 4 )
If e.Day.Date = myHoliday Then
e.Day.IsSelectable = True
EndIf
End Sub
To add content to an individual day
Dim Holidays ( 1 to 12, 1 to 31 ) as String
' Load holidays array during page load
Public Sub Page1_Load ( ByVal e as EventArgs )
Holidays ( 1, 1 ) = "New Year's Day"
Holidays ( 2, 14 ) = "Valentine's Day"
Holidays ( 12, 25 ) = "Christmas Day"
End Sub
Public Sub Calendar1_DayRender ( ByVal sender as Object, _
ByVal e as DayRenderEventArgs )
If e.DayCell.IsOtherMonth Then
e.DayCell.Controls.Clear
Else
Dim Hol as String
dt = new System.DateTime ( e.DayCell.Date )
Hol = Holidays ( dt.Month, dt.Day )
if Len ( Hol ) > 0 then
Dim labHoliday as New Label
labHoliday.Text = "<br>" & Hol
e.DayCell.Controls.Add labHoliday
End If
End If
End Sub
|
| ||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme