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

When the page is first loaded, we use the LoadMyCalendarData routine to load the calendar details from the XML file. This is exactly the same routine as we used earlier in the MyCalendar.aspx example, so we won't look at that again here.

Now let's look at the event procedures for editing, starting with the one for directly editing an entry. This is run when we select the Edit link, and its job is to tell the grid which row is being edited. It does this by setting the EditItemIndex property on the grid. Whenever this is set to a row, the EditItemTemplate for that row is displayed instead of the ItemTemplate. To identify the correct row, we use the arguments of the event procedure – these arguments are defined by ASP.NET, and provide information on which object called the event, along with various other sorts of information. In this case, the other information is the index number of the row that we are editing, as defined by the ItemIndex property. This is provided automatically by ASP.NET, because we have the link button in a row on the grid, whenever that link button is pressed the index number is supplied to the event procedure, thus allowing us to identify the correct row. As soon as the EditItemTemplate appears, we can make our changes:

Sub DEDR_Edit(Sender As Object, E As DataGridCommandEventArgs)                              
                       
 EventData.EditItemIndex = CInt(e.Item.ItemIndex)                              
 EventData.DataSource = LoadMyCalendarData                            
 EventData.DataBind()                          
                         
End Sub

To update a row, we have to extract the data we have inserted into the text boxes on the row. We once again obtain the current row number, and this will be used to index into the rows in the DataSet:

Sub DEDR_Update(Sender As Object, E As DataGridCommandEventArgs)                          
 Dim dataSet As DataSet = LoadMyCalendarData                            
 Dim row As Integer = CInt(e.Item.ItemIndex)

To find the information in the DataSet text boxes, we have to use the FindControl method. Although we have given our text boxes names, because they are used within a grid, these names could be ambiguous. So, when generating a grid, ASP.NET uses this name as part of a unique name for the controls on the grid. We don't know what the rest of this unique name is, therefore we have to use FindControl to find the correct control. Once we have found the row, we then update the data in the DataSet:

 Dim EditText As TextBox                            
                         
 EditText = E.Item.FindControl("txtShortDesc")                            
 dataSet.Tables(0).Rows(row).Item("ShortDesc") = EditText.Text                            
 EditText = E.Item.FindControl("txtDetailDesc")                            
 dataSet.Tables(0).Rows(row).Item("DetaiLDesc") = EditText.Text                              
 EditText = E.Item.FindControl("txtStartTime")                            
 dataSet.Tables(0).Rows(row).Item("StartTime") = EditText.Text                          
 EditText = E.Item.FindControl("txtEndTime")                            
 dataSet.Tables(0).Rows(row).Item("EndTime") = EditText.Text

At this stage the DataSet has been updated, but the data hasn't been written back to the XML file, so we use WriteXml to write the file out to disk:
dataSet.WriteXml(Server.MapPath("MyCalendar.xml"))
When we initially read the XML we placed it in Session state, to save having to read it again. Now that the data has changed it needs to be reloaded into the Session, however, so we remove the copy currently in the Session:
 Session("MyCalendarData") = Nothing
We then set the EditItemIndex of the grid to –1, which takes the grid out of edit mode. When this happens the EditItemTemplate is no longer used, and the row reverts back to using the ItemTemplate:
 EventData.EditItemIndex = -1
Finally, we reload the data into the grid:
EventData.DataSource = LoadMyCalendarData                            
 EventData.DataBind()                            
End Sub

That takes care of changing existing data, but what about canceling changes? When in edit mode, we have the Cancel link, which calls the following procedure. This is quite simple, first setting the EditItemIndex to –1, to take the grid out of edit mode. We then invalidate the Session state, and reload the data. Strictly speaking we don't always need to invalidate the Session variable here. For example, when editing a row, the changes are only available as part of the form – it's the Update procedure that updates the DataSet. So, we could just rebind to the cached data, which hasn't changed. When we add a row, however, we do update the DataSet – in this case just rebinding wouldn't work – we actually have to invalidate the Session state, and reload the data from the file:
Sub DEDR_Cancel(Sender As Object, E As DataGridCommandEventArgs)                            
 EventData.EditItemIndex = -1                            
 Session("MyCalendarData") = Nothing                        
 EventData.DataSource = LoadMyCalendarData                            
 EventData.DataBind()                            
End Sub

To delete a row, we just have to identify the row number selected (using the ItemIndex), and then use this to delete the selected row in the DataSet. Once deleted, we write the data to the XML file and invalidate the Session data because, the data has changed:
Sub DEDR_Delete(Sender As Object, E As DataGridCommandEventArgs)                            
                         
 Dim dataSet As DataSet = LoadMyCalendarData                            
 Dim row     As Integer = CInt(e.Item.ItemIndex)                            
                       
 dataSet.Tables(0).Rows(row).Delete                            
                       
 dataSet.WriteXml(Server.MapPath("MyCalendar.xml"))                            
                         
 Session("MyCalendarData") = Nothing

We then take the grid out of edit mode, and rebind the data:

 EventData.EditItemIndex = -1                            
                       
 EventData.DataSource = LoadMyCalendarData                            
 EventData.DataBind()                            
End Sub

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

Sponsored Links