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

10.The next section implements the method, ShowDailyEvents, that we referenced in steps 5 and 6. This method's task is to display the detailed information for all events (the detailed event description, the start time, the end time, and so on), based on the currently selected day in the calendar. This detailed display of calendar data will actually be rendered via a Repeater control, and will be displayed below the Calendar. In this way, we'll have a bit more real estate to display the detailed information about the selected day's events, which would normally be too verbose to display in within the Calendar control itself. Add the following lines in the <script> </script> tags:

<script language="VB" runat="server">                  
...                  
   Protected Sub ShowDailyEvents()                  
                 
   Dim d As Date = MyCalendar.SelectedDate()                  
   Dim dataSet as DataSet = LoadMyCalendarData()                      
   if dataSet is Nothing                        
     Exit Sub                      
   End if                      
                 
   Dim zRow as DataRow                      
   Dim aEvents as new ArrayList()                      
                 
   For Each zRow in dataSet.Tables(0).Rows                        
     Dim compareDate as DateTime                        
     compareDate = GetSafeDate ( zRow.Item("EventDate") )                        
                   
   If ( compareDate = d ) Then                          
                 
     ' Event matches date criteria -- display it...                          
     Dim myEventData as New MyCalendarEventData                          
     myEventData.EventDate = d                          
     myEventData.ShortDesc = zRow.Item("ShortDesc")                          
     myEventData.DetailDesc = zRow.Item("DetailDesc")                          
     myEventData.StartTime = zRow.Item("StartTime")                          
     myEventData.EndTime = zRow.Item("EndTime")                    
                       
     aEvents.Add ( myEventData )                        
   End If                      
 Next                      
                 
 ' Bind to the Repeater control...                      
 DailyEventDetailRepeater.DataSource = aEvents                        
 DailyEventDetailRepeater.DataBind()                      
                 
 If ( aEvents.Count > 0 ) Then                        
   DailyDetailsPanel.Visible = True                        
   SelectedDate.Text = "Events For " + d.ToLongDateString()                      
 Else                        
   DailyDetailsPanel.Visible = False                        
   SelectedDate.Text = "No Events Scheduled For " +                  
   d.ToLongDateString()                      
 End if                    
                 
End Sub                  
</script>

11.The MyCalendar.xml data source file could conceivably contain an invalid date entry in its EventDate XML element tag therefore, we'll add the following helper method to our page to guarantee that a non-null DateTime object is always returned, regardless of the date value obtained from the source XML:

<script language="VB" runat="server">                  
...                  
                 
 Private Function GetSafeDate ( ByVal proposedDate as String )                  
 As DateTime returns a non-null DateTime even if proposed                  
 date can't be parsed                      
 Dim safeDate as DateTime = DateTime.MinValue                      
 Try                        
   safeDate = DateTime.Parse ( proposedDate )                      
 Catch e As Exception                        
   Response.Write ( "<!-- Failed to parse date: " +                  
   e.Message + " -->" )                      
 End Try                      
                 
 GetSafeDate = safeDate                    
End Function                  
</script>

12.In this step, we add a MyCalendarEventData class implementation. This class serves as a container for the various data elements that make up our own custom calendar event data:

<script language="VB" runat="server">                  
...                    
Public Class MyCalendarEventData                      
                 
 Private m_ShortDesc As String                      
 Private m_DetailDesc As String                      
 Private m_EventDate As DateTime                      
 Private m_StartTime As String                      
 Private m_EndTime As String                      
 Public Property ShortDesc() As String                        
   Get                          
     Return m_ShortDesc                        
   End Get                        
   Set                          
   m_ShortDesc = value                        
   End Set                      
 End Property                      
                 
 Public Property DetailDesc() As String                        
   Get                          
     Return m_DetailDesc                        
   End Get                        
   Set                          
     m_DetailDesc = value                        
   End Set                      
 End Property                      
                 
 Public Property EventDate As DateTime                        
   Get                          
     Return m_EventDate                        
   End Get                        
   Set                          
     m_EventDate = Value                        
   End Set                      
End Property                  
 Public Property StartTime() As String                        
   Get                          
     Return m_StartTime                        
   End Get                        
   Set                          
     m_StartTime = value                        
   End Set                      
 End Property                    
                   
 Public Property EndTime() As String                        
   Get                          
     Return m_EndTime                        
   End Get                        
   Set                          
     m_EndTime = value                        
   End Set                      
 End Property                    
End Class                  
</script>

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

Sponsored Links