directory.aspx
来自「asp做的新闻系统」· ASPX 代码 · 共 122 行
ASPX
122 行
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.xml" %>
<script language="VB" runat="server">
Sub Page_Load(sender As object,e As EventArgs)
Dim ar As ArrayList=new ArrayList()
'获取当前目录的路径
Dim dr As String=Server.MapPath("./")
'获取当前目录下的所有XML文件
Dim fils() As string=Directory.GetFiles(dr,"*.xml")
Dim i As Integer
Dim Filename As String
Dim pos As Integer
pos=len(dr)+1
'显示当前目录下的新闻文件
For i=0 to fils.length-1
Filename=Mid(fils(i),pos)
If Filename<>"contents.xml" then
ar.Add(filename)
End If
Next
dg.DataSource=ar
dg.DataBind()
End Sub
Sub AlterItem(Sender As Object,e As DataGridCommandEventArgs)
If e.CommandSource.CommandName="update" Then
Dim href,path,content As String
Dim Xdoc As New XMLDocument()
Dim objnode As XMLNode
href=e.item.cells(1).text
path=Server.MapPath(href)
Xdoc.Load(path)
Try
'存储文件名
fname.text=path
'获取各个节点的值
objnode=Xdoc.SelectSingleNode("document")
title.text=objnode.childnodes(0).innertext
author.text=objnode.childnodes(2).innertext
abstract.text=objnode.childnodes(1).innertext
content=objnode.childnodes(3).innertext
'替换XML格式
content=content.Replace("< ","<")
content=content.Replace("> ",">")
content=content.Replace("</paragraph><paragraph>",chr(13)+chr(10))
content=content.Replace("</paragraph>","")
content=content.Replace("<paragraph>","")
cont.text=content
dis.visible=true
catch ee As Exception
Response.Write(ee.ToString())
End Try
End If
End Sub
sub Button1_Click(sender As object,e As EventArgs)
If page.IsValid Then
Dim Xdoc As New XMLDocument()
Dim objnode As XMLNode
Dim path,xmlfile As String
path=fname.text
Xdoc.Load(path)
'替换节点内容
objnode=Xdoc.SelectSingleNode("document")
objnode.childnodes(0).innertext=title.text
objnode.childnodes(2).innertext=author.text
objnode.childnodes(1).innertext=abstract.text
xmlfile=cont.text
'转换新闻内容格式
xmlfile=xmlfile.Replace("<","< ")
xmlfile=xmlfile.Replace(">","> ")
xmlfile=xmlfile.Replace (chr(13)+chr(10),"</paragraph><paragraph>")
xmlfile="<paragraph>" & xmlfile
xmlfile=xmlfile & "</paragraph>"
objnode.childnodes(3).innertext=xmlfile
xdoc.Save(path)
dis.visible=false
response.Write("修改成功!")
End If
End Sub
</script>
<html>
<title>使用DataGrid实现目录浏览</title>
</head>
<body>
<form method="post" runat="server">
<asp:DataGrid
Id="dg"
Runat="Server"
headerstyle-backcolor="#666699"
headerstyle-forecolor="#FFFFFF"
itemstyle-backcolor ="#EEEEEE"
alternatingitemstyle-backcolor="#FFFFFF"
alternatingitemstyle-forecolor="#000000"
OnItemCommand="AlterItem"
>
<Columns>
<asp:ButtonColumn HeaderText="Update" Text="修改" CommandName="update" />
</columns>
</asp:DataGrid>
<asp:Panel id="dis" visible=False Runat="server">
<p>修改新闻内容 </P>
<Asp:textbox id=fname visible=false runat="server"/>
文章名称:
<Asp:textbox id=title runat="server"/>
<Asp:RequiredFieldValidator id="valid2"
ControlToValidate="Title" runat="Server">(必要栏)
</Asp:RequiredFieldValidator><p>
作者:
<asp:textbox id=author runat="server"/>
<Asp:RequiredFieldValidator id="valid3"
ControlToValidate="author" runat="Server">(必要栏)
</Asp:RequiredFieldValidator><p>
摘要:<p>
<asp:textbox id=abstract textmode="MultiLine" width="70%" runat="server"/>
<p>
内容:<p>
<asp:textbox id=cont textmode="MultiLine" rows=6 width="70%" runat="server"/>
<p>
<asp:Button id="submit" text="提交" OnClick="Button1_Click" runat="server"/>
</asp:Panel>
</Form>
</Body>
</Html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?