Article

Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Next

7. The Calendar control in this page declares an event handler for the OnSelectionChanged event that we added in step 2. Remember that in step 2 the name of the event handler we assigned was MyCalendar_SelectionChanged. The implementation for this event handler should be added within the <script> </script> tags, and below the Page_Load method we just added, as follows:

<script language="VB" runat="server">                  
...                    
Public Sub MyCalendar_SelectionChanged(ByVal sender As Object, _                  
ByVal e As System.EventArgs)                      
   ShowDailyEvents()                  
 End Sub                  
</script>

When the user clicks a new date on the calendar control (thus triggering the OnSelectionChanged event), the MyCalendar_SelectionChanged event handler will call the ShowDailyEvents method. So this is almost functionally identical to our Page_Load implementation, above.

8. In order to display our custom calendar events within the Calendar control, we must write a method that loads the data from MyCalendar.xml into a DataSet object. This is implemented as follows (again, add this code in your <script> </script> tags, below the event handler we just added):

<script language="VB" runat="server">                  
...                    
Protected Function LoadMyCalendarData() As DataSet                      
               
   Dim sourceXML as String = Server.MapPath("MyCalendar.xml")                        
     If ( Not File.Exists ( sourceXML ) ) Then                          
       Return Nothing                  
     End if                  
               
   Dim cachedDataSet as DataSet = Session("MyCalendarData")                  
       if ( Not cachedDataSet Is Nothing ) Then                  
         Return cachedDataSet                  
       End if                  
   Dim dataSet As DataSet = New DataSet()                  
               
 Try                  
   dataSet.ReadXml(sourceXML)                  
               
   Session("MyCalendarData") = dataSet                  
               
 Catch e As Exception                  
   SelectedDate.Text = e.Message                  
   dataSet = Nothing                  
 End Try                  
                 
 Return dataSet                  
End Function                  
</script>

The key points to observe from this listing are that we first check to see if the file, MyCalendar.xml (that we defined, along with the location of the file, in the first line, as sourceXML) exists. If it doesn't exist on the server file system, we won't be able to display any of our custom calendar data in the Calendar or Repeater controls. Note also that we use the Session object to determine if we've already loaded (or cached) this DataSet object -- if so, we can use it and optimize our code slightly. We'll discuss this more in the How It Works section. The part of the code in the Try block loads the XML data into a DataSet object via the ReadXml method (which enables the XML in the file to be read). The Catch block checks that LoadMyCalendarData does return something when it is called, as it is possible that nothing could be returned, particularly if MyCalendar.xml contains any malformed data:

9. We are able to display our own data within the calendar by implementing an event handler for the Calendar control's OnDayRender event, which is raised each time a visible day in the calendar is being rendered. The MyCalendar_DayRender method (remember we introduced this in step 2) renders the Calendar control's Cell display by iterating through each record in the DataSet to determine if there is an event to display for the particular day being rendered. This method will also perform the work of setting various Cell.BackColor properties based on if the day being rendered is a weekend, weekday, or a day from a next or previous month. It is implemented as follows, and is to be placed, once again, within the <script> </script> tags:

<script language="VB" runat="server">                  
...                  
   Protected Sub MyCalendar_DayRender(ByVal Sender As System.Object, _                  
         ByVal e As DayRenderEventArgs )                    
   if ( e.Day.IsOtherMonth )                          
     e.Cell.BackColor=System.Drawing.Color.FromName("Gainsboro")                      
   Else If ( e.Day.IsWeekend )                        
     e.Cell.BackColor=System.Drawing.Color.FromName("PaleGoldenrod")                    
   Else                        
     e.Cell.BackColor=System.Drawing.Color.FromName                
     ("LightGoldenrodYellow")                    
 End if                    
               
Dim dataSet as DataSet = LoadMyCalendarData()                      
 if dataSet is Nothing                        
   Exit Sub                      
 End if                    
               
Dim zRow as DataRow                      
               
 For Each zRow in dataSet.Tables(0).Rows                        
   Dim compareDate as DateTime                        
   compareDate = GetSafeDate ( zRow.Item("EventDate") )                        
                   
   If ( compareDate = e.Day.Date ) Then                      
                   
     ' Event matches date criteria -- display it...                        
     Dim myEventData as New MyCalendarEventData                          
     myEventData.ShortDesc = zRow.Item("ShortDesc")                          
     myEventData.DetailDesc = zRow.Item("DetailDesc")                        
     myEventData.StartTime = zRow.Item("StartTime")                        
     myEventData.EndTime = zRow.Item("EndTime")                      
                 
     Dim dailyEventLabel as New Label                          
     dailyEventLabel.Text = "<br />" + myEventData.ShortDesc                      
     e.Cell.Controls.Add ( dailyEventLabel )                        
   End if                    
 Next                  
               
End Sub                  
</script>

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links