原作:不详
完善:大鼹鼠
再完善:塞北的雪
但是现在还不能对一个没有记录的xml进行操作。但是会提示空。
另外做完一个删除操作后,如果刷新也会出错,因为我对vb.net不是很熟,所以改的不知道怎么样?
<%@page Language="VB" runat="server" responseEncoding="utf-8"%>
<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.IO" %>
<%@ import Namespace="System.Xml" %>
<script Language="VB" runat="server" >
'原来responseEncoding=gb2312 然而通过更新操作的汉字就会将乱码保存到xml文件中
'改成responseEncoding=utf-8 就没有问题了。
Sub Page_Load(sender As Object,E As EventArgs)
if Not Page.IsPostBack then
EventDate.DataSource = LoadMyConfigdata
if intRecordCount> 0 then
EventDate.DataBind()
else
Response.Write("这是一个空XML文件")
Response.End()
end if
end if
End Sub
Private intRecordCount As Integer
Protected Function LoadMyConfigdata() As DataSet
Dim sourceXML as String = server.MapPath("lzdata.xml")
if (Not File.Exists(sourceXML)) then
Return Nothing
end if
Dim doc As New XmlDocument()
doc.Load(SourceXML)
intRecordCount=doc.selectNodes("//groups").Count
Dim cachedDataSet as DataSet = Session("MyConfigData1")
if (Not cachedDataSet is Nothing) Then
Return cachedDataSet
end if
Dim dataSet as DataSet = New DataSet()
try
dataSet.ReadXml(SourceXML)
Session("MyConfigData1")=dataSet
catch e As Exception
ErrorMessage.Text = e.Message
dataSet=Nothing
end try
Return dataSet
End Function
Sub DEDR_Edit(Sender As Object,E as DataGridCommandEventArgs)
EventDate.EditItemIndex = Cint(E.Item.ItemIndex)
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
End Sub
Sub DEDR_Update(Sender As Object,E as DataGridCommandEventArgs)
Dim dataSet As DataSet = LoadMyConfigData
Dim P1 As Integer=EventDate.CurrentPageIndex '获得页数 (页数是从0开始的)
Dim Row1 As Integer = Cint(e.Item.ItemIndex) '获得发生事件的行数 (从0开始的)
Dim row As Integer
Dim EditText As TextBox
row = P1* EventDate.PageSize +Row1
EditText=E.Item.FindControl("txtTitle") '获得数据采集文本框
dataSet.Tables(0).Rows(row).Item("Title") = Trim(EditText.Text)
EditText=E.Item.FindControl("txtFolder")
dataSet.Tables(0).Rows(row).Item("Folder") = Trim(EditText.Text)
EditText=E.Item.FindControl("txtFolderimg")
dataSet.Tables(0).Rows(row).Item("Folderimg") = Trim(EditText.Text)
EditText=E.Item.FindControl("txtName")
dataSet.Tables(0).Rows(row).Item("Name") = EditText.Text
dataSet.WriteXml(server.MapPath("lzdata.xml"))
Session("MyConfigData1")=Nothing
EventDate.EditItemIndex= -1
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
End Sub
Sub DEDR_Cancel(Sender As Object,E as DataGridCommandEventArgs)
EventDate.EditItemIndex= -1
Session("MyConfigData1")=Nothing
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
End Sub
Sub DEDR_Delete(Sender As Object,E as DataGridCommandEventArgs)
Dim dataSet As DataSet = LoadMyConfigData
Dim Row1 As Integer = Cint(e.Item.ItemIndex)
Dim P1 As Integer=EventDate.CurrentPageIndex
Dim row As Integer
if (intRecordCount mod EventDate.PageSize)=1 and Row1=0 then
'response.write ("<script>alert('每页只剩一个记录时不能删除,请到上一页删除,或在该页添加一个新记录再删除!')</" & "script>")
row = P1* EventDate.PageSize +Row1
dataSet.Tables(0).Rows(row).Delete
dataSet.WriteXml(server.MapPath("lzdata.xml"))
Session("MyConfigData1")=Nothing
EventDate.EditItemIndex= -1
if EventDate.CurrentPageIndex>0 then
EventDate.CurrentPageIndex=EventDate.CurrentPageIndex-1
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
else
Response.Write("Xml已经被删空了")
Response.End()
end if
else
row = P1* EventDate.PageSize +Row1
dataSet.Tables(0).Rows(row).Delete
dataSet.WriteXml(server.MapPath("lzdata.xml"))
Session("MyConfigData1")=Nothing
EventDate.EditItemIndex= -1
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
end if
End Sub
Sub DEDR_Add(Sender As Object,E as EventArgs)
Dim dataSet As DataSet = LoadMyConfigData
Dim newRow As DataRow '建立一个新行
newRow=dataSet.Tables(0).NewRow()
newRow.Item("EventDate")=Now() '"15/07/2001"
newRow.Item("Title")="讨论和解决"
newRow.Item("Folder")="group"
newRow.Item("Folderimg")="images/BigMolefolder.gif "
newRow.Item("Name")="技术"
dataSet.Tables(0).Rows.Add(newRow) '将行添加到当前的dataset中
dataSet.WriteXml(server.MapPath("lzdata.xml")) '将dataset写到xml文件中
Session("MyConfigData1")=Nothing
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
'每次都至多有一行被置为编辑行,编辑行显示的就是列中的 EditItemTemplate
dim ps as Integer=intRecordCount '记录数
EventDate.CurrentPageIndex=Math.Ceiling(ps/EventDate.PageSize)-1 '设定当前显示最后一页
dim tmpInt as Integer=ps-Math.Floor(ps/EventDate.PageSize)*EventDate.PageSize
if tmpInt=0 then tmpInt=EventDate.PageSize
EventDate.EditItemIndex= tmpInt - 1 '设置显示EditItemTemplate的行号
EventDate.DataSource = LoadMyConfigData
EventDate.DataBind()
End Sub
'翻页时
Sub DEDR_Changed(sender As Object,e As DataGridPageChangedEventArgs)
EventDate.EditItemIndex=-1
EventDate.CurrentPageIndex = e.NewPageIndex
EventDate.DataSource = Session("MyConfigData1")
EventDate.DataBind()
End Sub
</script>
<HTML>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<BODY>
<% '下面的列设置中,每一个 TemplateColumn中都有 ItemTemplate 和 EditItemTemplate两种 可以
'通过代码控制显示哪一种 %>
<form runat="server">
<asp:Label id="errorMessage" runat="server"/><br/>
<asp:LinkButton OnClick="DEDR_Add" Text="添加新记录" runat="server"/><br/>
<asp:DataGrid id="EventDate"
AutoGenerateColumns="false" width="100%" runat="server"
AllowPaging="True"
PageSize="15"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
PagerStyle-PrevPageText="前页"
PagerStyle-NextPageText="后页"
OnPageIndexChanged="DEDR_Changed"
OnEditCommand="DEDR_Edit"
OnUpdateCommand="DEDR_Update"
OnCancelCommand="DEDR_Cancel"
OnDeleteCommand="DEDR_Delete">
<HeaderStyle ForeColor="white" BackColor="DodgerBlue"
Font-Bold="true"/>
<ItemStyle BackColor="white"/>
<AlternatingItemStyle BackColor="Gainsboro"/>
<Columns>
<asp:TemplateColumn HeaderText="栏目描述">
<ItemTemplate>
<%# Container.DataItem("Title")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtTitle" size="25"
Text='<%# Container.DataItem("Title")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="栏目文件目录">
<ItemTemplate>
<%# Container.DataItem("Folder")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtFolder" size="14"
Text='<%# Container.DataItem("Folder")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="栏目图标">
<ItemTemplate>
<%# Container.DataItem("Folderimg")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtFolderimg" size="24"
Text='<%# Container.DataItem("Folderimg")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="栏目名称">
<ItemTemplate>
<%# Container.DataItem("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" size="15"
Text='<%# Container.DataItem("Name")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="操作">
<ItemTemplate>
<asp:LinkButton CommandName="Edit" Text="编辑"
runat="server"/>
<asp:LinkButton CommandName="Delete" Text="删除"
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Cancel" Text="取消"
runat="server"/>
<asp:LinkButton CommandName="Update" Text="更新"
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
<!--
列中的 几个linkButton 的 CommandName 可以取 Edit,Delete,Cancel,Update 这个名字不能乱取
他和DataGrid的 onEditCommand onUpdateCommand onDeleteCommand onCancelCommand 是相对应的
-->
</BODY>
本文地址:http://com.8s8s.com/it/it8570.htm