您的位置:首页 > 编程语言 > ASP

用Asp.net实现基于XML的留言簿 【转】

2006-08-17 18:20 489 查看
一.概要:

  留言簿是网站的一个重要组成部分,是访问者发表意见的场所,也是网站管理员了解网站基本情况的有力工具。所以留言簿在现在的网站中扮演了重要的角色。

  可是在以前开发一个留言簿并不是一件容易的事,开发者的工作量往往会很多。而现在随着微软推出VS.net,相应的技术推陈出新。特别是XML在.net Framework中的广泛运用,使得整个.net构架具有十分优越的基础。在VS.net推出的同时,也伴随而来了一门新兴的语言C#。C#作为微软.net战略的重要部分,具有特别优秀的性能。所以本文的asp.net程序是用C#语言描述的,同时整个程序又是基于XML的。我用到了XML作为程序的数据库,主要是因为所以的服务器都是支持XML文件的。

二.要求:

(1) .Net SDK Beta2及以后版本

(2) 支持Asp.net的Web服务器

三.说明:

本文的实例由两部分组成:

(1) guestpost.aspx-将用户信息添加到一个XML文件中

(2) viewguestbook.aspx-先建立一个数据集对象,建立后,就很容易显示其中的数据了。我这里用到了一个“Repeater”来显示数据集中的数据。还有,读者可试着根据自己的喜好来修改查看页。

四.代码:

以下为引用的内容:

(1) guestpost.aspx:

< %@ Page Language="C#" EnableSessionState="False" % >
< %@ Import Namespace="System" % >
< %@ Import Namespace="System.IO" % >
< %@ Import Namespace="System.Data" % >
< %-- 这些是本程序正常运用所必须的名字空间 --% >

< html >
< head >
< title >欢迎来到我的留言簿< /title >
< script Language="C#" runat="server" >
///< summary >
/// 当提交(submit)按钮按下后,调要这个函数
///< /summary >
public void Submit_Click(Object sender, EventArgs e)
{
//保存数据的XML文件的路径
//如果你的路径和下面的不同,则请修改之
string dataFile = "db/guest.xml" ;

//运用一个Try-Catch块完成信息添加功能
try{
//仅当页面是有效的时候才处理它
if(Page.IsValid){

errmess.Text="" ;
//以读的模式打开一个FileStream来访问数据库
FileStream fin;
fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,
FileAccess.Read,FileShare.ReadWrite);
//建立一个数据库对象
DataSet guestData = new DataSet();
//仅从数据库读取XML Schema
guestData.ReadXmlSchema(fin);
fin.Close();
//从数据集的Schema新建一个数据行
DataRow newRow = guestData.Tables[0].NewRow();
//用相应值填写数据行
newRow["Name"]=Name.Text;
newRow["Country"]=Country.Text;
newRow["Email"]=Email.Text;
newRow["Comments"]=Comments.Text;
newRow["DateTime"]=DateTime.Now.ToString();
//填写完毕,将数据行添加到数据集
guestData.Tables[0].Rows.Add(newRow);
//为数据库文件新建另一个写模式的FileStream,并保存文件
FileStream fout ;
fout = new FileStream(Server.MapPath(dataFile),FileMode.Open,
FileAccess.Write,FileShare.ReadWrite);

guestData.WriteXml(fout, XmlWriteMode.WriteSchema);
fout.Close();
//隐藏当前的面板
formPanel.Visible=false;
//显示带有感谢信息的面板
thankPanel.Visible=true;
}
}
catch (Exception edd)
{
//捕捉异常
errmess.Text="写入XML文件出错,原因:"+edd.ToString() ;
}
}
< /script >
< LINK href="mystyle.css" type=text/css rel=stylesheet >
< /head >
< body >
< %-- 包含一个头文件:header.inc --% >
< !-- #Include File="header.inc" -- >
< br >
< h3 align="center" class="newsbody" >留言者信息< /h3 >
< br >
< asp:label id="errmess" text="" style="color:#FF0000" runat="server" / >
< asp:Panel id=formPanel runat=server >
< form runat="server" >
< table border="0" width="80%" align="Center" >
< tr >
< td class="newsheading" >< b >请在我留言簿留下您宝贵的信息!!< /b >< /td >
< td class="newsheading" > < /td >
< /tr >
< tr class="newsbody" >
< td >姓名:< /td >
< td >< asp:textbox text="" id="Name" runat="server" / >
< asp:RequiredFieldValidator ControlToValidate=Name display=static
runat=server >
*< /asp:RequiredFieldValidator >< /td >< /tr >
< tr class="newsbody" >< td >国家:< /td >
< td >< asp:textbox text="" id="Country" runat="server"/ >
< asp:RequiredFieldValidator ControlToValidate=Country display=static
runat=server >
*< /asp:RequiredFieldValidator >< /td > < /tr >
< tr class="newsbody" >< td >E-Mail:< /td >
< td >< asp:textbox test="" id="Email" runat="server"/ >
< asp:RequiredFieldValidator ControlToValidate=Email display=static
runat=server >
*< /asp:RequiredFieldValidator >< asp:RegularExpressionValidator
runat="server"
ControlToValidate="Email"
ValidationExpression="[\w-]+@([\w-]+\.)+[\w-]+"
Display="Static"
Font-Name="verdana" Font-Size="10pt" >
请输入一个格式正确的Email地址!< /asp:RegularExpressionValidator >< /td >
< /tr >< tr class="newsbody" >< td >留言:< /td >
< td >< asp:Textbox textmode=multiline id="Comments" columns="25"
rows="4" runat="server" / >< /td >< /tr >
< tr class="newsbody" >
< td colspan="2" >
< asp:Button class="newsheading" id="write" Text="Submit"
onClick="Submit_Click" runat="server"/ >< /td >< /tr >< /table >< /form >< /asp:Panel >

< asp:Panel id=thankPanel visible=false runat=server >
< p class="newsbody" align=center >< b >谢谢访问我的留言簿!< /b >
< br >< a href="viewguestbook.aspx" >请点击这里 < /a > 查看留言簿。
< /p >
< /asp:Panel >
< !-- #Include File="footer.inc" -- >
< /body >
< /html >

以下为引用的内容:

(2) viewguestbook.aspx:

< %@ Page Language="C#" % >
< %@ Import Namespace="System" % >
< %@ Import Namespace="System.IO" % >
< %@ Import Namespace="System.Data" % >
< %-- 以上是所需的名字空间 --% >

< html >
< head >
< title >欢迎来到我的留言簿< /title >
< script language="C#" runat=server >
//页面下载完毕后,运行这个脚本
public void Page_Load(Object sender, EventArgs e)
{
//包含所有数据的XML文件的路径
//如果你的路径和下面的不同,则请修改
string datafile = "db/guest.xml" ;

//运用一个Try-Catch块完成信息读取功能
try
{
//建立一个数据集对象
DataSet guestData = new DataSet();
//为数据库文件打开一个FileStream
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile),FileMode.Open,
FileAccess.Read,FileShare.ReadWrite) ;
//把数据库中内容读到数据集中
guestData.ReadXml(fin);
fin.Close();
//将第一个表中的数据集付给Repeater
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 >

五.总结:

  这个程序完成了,相信大家对实现原理以及技巧都有了大致的了解。通过本文,我主要是想向大家展示用XML处理一些数据库问题时的优点:服务器都支持XML文件,而且处理过程相当简洁明了。当然,用XML也有它的不足之处,就是当数据库很大时,解析过程会花费相当长的时间,因此还是要采用大型的数据库系统的。所以,我只想通过此文起到抛砖引玉的作用。

程序运行后的图示如下:



转自:动态网站制作指南 | www.knowsky.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: