65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 GridView 编辑 XML 文件

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (10投票s)

2007年4月4日

CPOL

3分钟阅读

viewsIcon

132906

downloadIcon

1698

使用 GridView 编辑 XML 文件。

Screenshot - 027_edit_xml.jpg

引言

这篇简短的文章展示了如何使用 GridView 控件编辑 XML 文件。 此外,用户可以使用一些文本字段和一个按钮创建新记录。

XML 文档

让我们看看 XML 文件的结构

<?xml version="1.0" standalone="yes"?>
<Mitarbeiter>
  <ma>
    <id>1</id>
    <name>Florian 1</name>
    <phone>345</phone>
  </ma>
  ...
</Mitarbeiter>

该文档包含一些保存个人数据的记录。 每个记录都有一个 ID、名称和一个电话号码。

Web 窗体

首先,我们需要一些用于 Web 窗体的控件。 我们需要一个 GridView 来显示、编辑和删除 XML 文件中的记录。 GridView 将包含两个命令字段:ShowEditButtonSchowDeleteButton。 为了创建新条目,我将使用三个 TextBox 字段来表示记录的列和一个 Button。 这是表单的代码

<%@ Page Language="VB" AutoEventWireup="false" 
  CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <br />
        <asp:TextBox ID="txtId" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtTel" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

Page_Load 和 PageIndexChanging

PageLoad 事件中,我必须将 XML 文件绑定到 GridView。 为此,我创建了一个例程 BindGrid(),因为我需要多次绑定 GridView。 在 BindGrid 方法中,使用 PhysicalApplicationPath 获取 XML 文件的完整路径。 这是代码

Protected Sub Page_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load
   If Page.IsPostBack = False Then
      BindGrid()
   End If
End Sub

Sub BindGrid()
   Dim oDs As New DataSet
   oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
   GridView1.DataSource = oDs
   GridView1.DataBind()
End Sub

接下来,我必须创建一个事件处理程序来实现 GridView 的分页。 这很简单,只需将 GridViewPageIndex 设置为 e.NewPageIndex

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, _
         ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
         Handles GridView1.PageIndexChanging
   GridView1.PageIndex = e.NewPageIndex
   BindGrid()
End Sub

删除记录

从文件中删除记录很容易。 我将 GridView 中的 DataSource 分配给一个 DataSet 对象。 然后,我使用 GridView 中的 DeleteButton 删除用户选择的记录。 最后,我更新 XML 文件并调用 BindGrid()

Protected Sub GridView1_RowDeleting(ByVal sender As Object, _
       ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) _
       Handles GridView1.RowDeleting
   BindGrid()
   Dim oDs As DataSet = GridView1.DataSource
   oDs.Tables(0).Rows(GridView1.Rows(e.RowIndex).DataItemIndex).Delete()
   oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
   BindGrid()
End Sub

更新记录

更新记录需要更多的工作。 首先,我需要使用 e.NewEditIndex 设置 GridViewEditIndex 并再次调用 BindGrid。 这必须在 RowEditing 事件中完成

Protected Sub GridView1_RowEditing(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
        Handles GridView1.RowEditing
   GridView1.EditIndex = e.NewEditIndex
   BindGrid()
End Sub

然后,我实现 RowCancelingEvent。 非常简单:我只需要将 EditIndex 设置为 -1 并绑定 GridView

Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, _
       ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) _
       Handles GridView1.RowCancelingEdit
   GridView1.EditIndex = -1
   BindGrid()
End Sub

现在我需要为 RowUpdating 事件创建代码。 当此事件触发时,我必须从 GridView 控件中获取记录的更改值到临时变量中。 这是使用从单元格到文本框的转换(CType)完成的。 使用方法 .Text 返回控件的当前值。 也许,使用 FindControl 更好。 无论如何,这是代码

Protected Sub GridView1_RowUpdating(ByVal sender As Object, _
         ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
         Handles GridView1.RowUpdating
   ' Get the new values from the GridView controls
   Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
   Dim strId As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
   Dim strName As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
   Dim strTel As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
   GridView1.EditIndex = -1
   BindGrid()
   ' Update the XML file using the new values

   Dim oDs As DataSet = GridView1.DataSource
   oDs.Tables(0).Rows(i).Item(0) = strId
   oDs.Tables(0).Rows(i).Item(1) = strName
   oDs.Tables(0).Rows(i).Item(2) = strTel
   oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
   BindGrid()
End Sub 

当我获得更改的值时,我将 EditIndex 重置为 -1 并绑定 GridView。 然后,我将 GridView 中的 DataSource 分配给 DataSet 并更新 DataSet 中的值。 最后,将新的 DataSet 写入 XML 文件。

创建新记录

最后,我必须创建新记录。 为此,我使用三个 TextBox 和一个 Button。 单击按钮时,我从 GridViewDataSource 创建一个 DataSet。 接下来,我调用方法 Tables(0).NewRow 并将值分配给创建的 DataRow。 最后,我将新的 DataRow AddDataSet 并使用 oDs.WriteXml 写入 XML 文件。

Protected Sub Button1_Click(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles Button1.Click
   BindGrid()
   Dim oDs As DataSet = GridView1.DataSource
   Dim oDr As DataRow = oDs.Tables(0).NewRow
   oDr("id") = txtId.Text
   oDr("name") = txtName.Text
   oDr("phone") = txtTel.Text
   oDs.Tables(0).Rows.Add(oDr)
   oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
   BindGrid()
End Sub

创建新记录

就是这样 - 将数据存储在 XML 文件中非常容易。 它也非常适合部署,因为我们可以将文件复制到所需的位置。 但是,关于此解决方案的简短警告:它不支持事务。 最后一个存储文件的人将获胜!!!

您可能对使用 GridView 编辑 XML 的屏幕广播感兴趣:http://dotnet-visions.de.z.seekdotnet.com/blog/index.php?entry=entry061207-022305 (仅限德语)。

© . All rights reserved.