📄 viewguestbook.aspx
字号:
<%--
viewguestbook.aspx
这个文件是用于查看留言簿的内容的,该例展示了如何打开、读、写一个XML文件。
使用 Repeater 去显示留言簿包含的内容
--%>
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%-- 所需要的集合 --%>
<html>
<head>
<title>第四十四例 基于XML的留言簿 </title>
<script language="C#" runat=server>
//当页面被装载时执行该脚本
public void Page_Load(Object sender, EventArgs e)
{
//该路径下的 Xml 文件包含了所有的数据
string datafile = "db/guest.xml" ;
//使用try-Catch 模块读 XML 文件
try
{
//生成一个DataSet 对象
DataSet guestData = new DataSet();
//为操作数据库,打开一个 FileStream
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ;
//读出数据库的内容到 DataSet 中
guestData.ReadXml(fin);
fin.Close();
//数据绑定到表格
MyDataList.DataSource = guestData.Tables[0].DefaultView;
MyDataList.DataBind();
}
catch (Exception edd)
{
//产生任何例外的时候出现
errmess.Text="不能读出XML文件,因为 "+edd.ToString() ;
}
}
</script>
<LINK href="mystyle.css" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" rightmargin="0">
<!-- #Include File="header.inc" -->
<asp:label id="errmess" text="" style="color:#FF0000" runat="server" />
<br>
<h3 align="center" class="newsbody"> 留 言 簿 内 容</h3>
<ASP:Repeater id="MyDataList" runat="server">
<headertemplate>
<table class="mainheads" width="100%" style="font: 8pt verdana">
<tr style="background-color:#FF9966">
<th>
名字
</th>
<th>
城市
</th>
<th>
Email
</th>
<th>
内容
</th>
<th>
日期、时间
</th>
</tr>
</headertemplate>
<itemtemplate>
<tr style="background-color:#FFFFCC">
<td>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Email") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Comments") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "DateTime") %>
</td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</ASP:Repeater>
<!-- #Include File="footer.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -