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

The code to render the MyCalendar.xml data to a specific Cell within the Calendar control is fairly straightforward. We loop through all the rows in our DataSet object's default Table and compare with the date the Calendar control is currently rendering -- if the dates are the same, we have some work to do. Remember that each row represents a single event, and each time a day is rendered we have to see if that day matches one of the events. Otherwise, we loop through to the next item. When we encounter a match, we actually add the content to the Calendar object's Cell property, by creating a new Label object, setting its display properties, and adding it to the Cell object's Controls container:

Protected Sub MyCalendar_DayRender(ByVal Sender As System.Object, _                    
        ByVal e As DayRenderEventArgs )                    
...                    
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 dailyEventLabel as New Label                    
   dailyEventLabel.Text = "<br />" + myEventData.ShortDesc                    
   e.Cell.Controls.Add ( dailyEventLabel )                    
 End If                    
Next                    
                   
End Sub

Repeater Control Implementation Details

The sole reason for implementing a Repeater control is to display the specific MyCalendar.xml event details that correspond to the currently selected day in the calendar. The details of an event or an appointment could contain quite a bit of information, therefore trying to put it all into a single Cell object of the Calendar control probably wouldn't look all that great. Displaying a short description (ShortDesc) in the Calendar control's Cell, however, and relegating the detailed description (DetailDesc), and any additional information to the Repeater is prudent.

The mapping of the MyCalendar.xml data to the Repeater control is handled by the ShowDailyEvents method, which is invoked when the page first loads, and also as a result of an OnSelectionChanged event posted by the Calendar control. This event is automatically reaised when the day (or month) selected is changed by the user. Recall from step 10 that we declared a class, MyCalendarEventData. This contained various Private members and corresponding Public properties, which served as a container for the data extracted from the MyCalendar.xml file:

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                    
  ...

There are at least two reasons for going through the effort of creating this class. The first is purely related to object oriented design. In the event that the MyCalendar.xml file changes, or if new elements are added, it's good practice to have an object, like MyCalendarEventData, to map the new values to. This way, we would only have to make the changes under the relevant sections of our XML file – the method that calls these sections would still work.

The second reason is interrelated with the first, but is actually more practical: we can bind MyCalendarEventData objects to any data rendering control, like a Repeater control (which we did in steps 8 and 9).

This brings us back to the ShowDailyEvents method. When invoked, this method performs a similar set of tasks to the MyCalendar_DayRender method, with a key difference: it renders all the matching events for a given day to the Repeater control, not the Calendar control. The ShowDailyEvents method proceeds to loop through all the DataRow objects in the DataSet object's default table in order to build an ArrayList of MyCalendarEventData objects. We need to do this because the DataSet contains all events, and not just those for the selected day. So, we build an ArrayList containing just those events for the selected day. As you can see in the code we added in step 9:

Protected Sub ShowDailyEvents()                        
 ...                      
 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                    
   ...                      
     aEvents.Add ( myEventData )                          
   End If                      
 Next                      
                   
 ' Bind to the Repeater control...                        
 DailyEventDetailRepeater.DataSource = aEvents                        
 DailyEventDetailRepeater.DataBind()                    
...                      
 End Sub

The significance of storing the matching MyCalendarEventData objects in the aEvents variable (an ArrayList containing just those events for the selected day) is that we can bind this sub-set of records to the DailyEventDetailRepeater object to generate a detailed listing of the events for the selected day.

The final details concerning the Repeater control implementation deal with display issues. For some days, there may be no specific events. As such, when the user clicks on a particular day Cell link of the Calendar control that has no related events, we implement the following code to toggle the visibility of the Panel control (which contains the DailyEventDetailRepeater object):

Protected Sub ShowDailyEvents()                      
 ...                      
 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

The SelectedDate object is a Label control that is not declared within the DailyDetailsPanel declaration, and therefore will not be affected when the DailyDetailsPanel.Visible property is changed. The DailyEventDetailRepeater control's visibility status, however, this will correspond to whatever the DailyDetailsPanel.Visible property is. This is a nice feature to use, especially when you want to control the visibility state of a group of controls with one single property change.

Editing the Data Using Templates

In Chapter 13, we looked at updating data in DataSets, and in this chapter we've looked at using the grids and templates. One of the most powerful features behind ASP.NET is the templating architecture, which allows us to define a different set of controls depending upon the actions of the user. For the DataGrid and DataList we can use the EditItem template we saw earlier to automatically display different controls when the user wishes to edit some data.

Let's look at a simple example to see how this works.

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

Sponsored Links