When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, November 26, 2003

Quickly Editing an XML File, Part 2

By Zubair Ahmed and Scott Mitchell

  • Read Part 1

  • In Part 1 we looked at the Page_Load event handler and the LoadXML()method, as well as the DataGrid's declaration. In this final part we'll look at the DataGrid's event handlers and see how everything is tied together.

    - continued -

    Examining the DataGrid's Event Handlers

    Since the Delete button's CommandName has been set to Delete whenever the Delete button is pressed, the DataGrid's DeleteCommand event is fired upon postback. Since in the DataGrid's declaration we wired up this event to the DelXML event handler, DelXML will execute whenever this button is pressed.

    I have decided that if the DataGrid is in edit mode then the user cannot delete a record until canceling out of edit mode. Therefore, in the DelXML event handler, I first check that the DataGrid is not in the edit mode (by making sure EditItemIndex = -1). If it is not, the Else segment gets executed, which displays a Label Web control mentioning the error. If there is no row being edited, though, I make sure the error Label is hidden.

    In addition, I reference the DataGrid's DataKeys collection to obtain the primary key value of the DataGrid row that is being deleted.

    Sub DelXML(S as Object, E as DataGridCommandEventArgs)
      If datagrid1.EditItemIndex=-1 Then
         Dim x1 as string
         x1 = DataGrid1.DataKeys.Item(e.Item.ItemIndex)

    Next the XML file sample.xml is read into a DataSet and the record is located and deleted.

         Dim objdata as New DataSet
            objdata.Tables("record").DefaultView.RowFilter="aid='" & x1 & "'"
            If objdata.Tables("record").DefaultView.Count > 0 Then
            End If
         End Try
       End If
    End Sub

    The above code deletes the record from the XML file by the following steps:

    1. First, the XML data is read into a DataSet
    2. Next, the DataSet's DataView's RowFilter property is used to filter the results down to the one record whose primary key matches the primary key of the record whose Delete button was clicked
    3. The singled out record is deleted from the DataSet
    4. The RowFilter property is reset
    5. The contents of the XML file are written back to disk, overwriting the previous version of the XML file

    When the Add LinkButton is clicked, the DataGrid's ItemCommand event is fired and the associated event handler - doInsert - executes. doInsert, as its name suggests, inserts a new record into the XML document. It makes a couple of checks to ensure that the person is not entering a record with a duplicate primary key value. Below is a shorted version of the doInsert method - some code has been removed for brevity.

     Sub doInsert(s as Object, E as DataGridCommandEventArgs)
        If e.CommandName="doAdd" Then
          Dim v1 as string
          Dim tadd1,tadd2, ... as Textbox
          tadd1 = e.Item.FindControl("aid_add")
          tadd2 = e.Item.FindControl("name_add")
          If tadd1.Text<>"" and tadd2.Text<>"" and ... Then
            objdata.Tables("record").DefaultView.RowFilter= "aid='" & _
                                                       tadd1.Text & "'"
            If objdata.Tables("record").DefaultView.Count <=0 Then
              'There is no duplicate data, go ahead and add the record
              objdata.Tables("record").DefaultView.RowFilter = ""
              'There is a duplicate
            End If
          End If
        End If
     End Sub

    This code reads in the values from the TextBox Web controls in the footer, ensures that there does not already exist a record with the same primary key field value, adds the record to the DataSet, and then writes the DataSet's contents back out to an XML file. After doing this, the LoadXML() method is called to rebind the new XML data to the DataGrid.

    The last final piece of the puzzle - providing editing support, works in a similar manner to the adding a new record feature. Namely, when the edited row's Update button is clicked, the event handler picks out the edited values of the edited row, reads in the XML file into a DataSet, locates the record that was updated, updates the fields, and then writes the XML file back to disk. Due to its similarity to inserting a record, the event handlers for editing a record are not discussed here - you can check them out in the code download at the end of this article. (For some background reading on creating an editable DataGrid, read: An Extensive Examination of the DataGrid Web Control: Part 6.)

    Changes Required For Different XML Files
    If you are using a different XML file, you will need to change the insert and update event handlers. Specifically, you will need to change it so that they read from the appropriate TextBox Web controls and insert into/update the appropriate fields in the XML file.

    Conclusion Alright, so now you have an easy way to update, create and store your records as XML content as well as modify the data later. With the help of the DataGrid server control we were able to make a professional-looking user interface with a minimal amount of code and effort. An XML file is not a replacement for a database, however it is designed to be a supplement to a database. For simplicity, I have not included any validation controls, sorting, or paging of the DataGrid, although these could be added.

    Be sure to view the live demo!

    Happy Programming!

  • By Zubair Ahmed and Scott Mitchell


  • Download the complete source code

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article