硬件学院 | 网络学院 | 游戏秘籍 | 求职技巧 | 企业管理 | 软件资讯 | IT导购 | 软件下载 | 源码下载
软件学院 | 安全资讯 | 图形图象 | 网络营销 | 电子商务 | 硬件资讯 | IT生活 | 教程下载 | 电影娱乐
网站首页    个人求职    单位招聘    高校联盟    猎头服务    培训服务    资讯中心    IT论坛
让每一个热爱IT的人都找到一份满意的工作!
文章搜索:
 您的位置首页->-> 软件学院-> .NET技术-> 用Asp.net实现基于XML的留言簿之二
用Asp.net实现基于XML的留言簿之二
作者:中国资讯网 来源:zixuen.com 加入时间:2005-5-12 www.cnitrc.com
四.代码:

(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 >



  相关文章:
.NET技术
ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库技术
其它类
工具软件
办公软件
本类阅读TOP10
 
关于我们   |   服务声明   |   使用帮助   |   广告合作   |   网站地图   |   友情链接   |   加盟合作   |   联系我们
Copyright © 2006 cnitrc.com Inc. All Rights Reserved. 浙ICP备05074295号
中国IT人才网 版权所有 网络实名:中国IT人才
未经书面授权严禁转载和复制本站的任何招聘信息和文章