用Asp.net實現基於XML的留言簿之二

來源:互聯網
上載者:User

四.代碼:

(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)
{
file://保存資料的XML檔案的路徑
file://如果你的路徑和下面的不同,則請修改之
string dataFile = "db/guest.xml" ;

file://運用一個Try-Catch塊完成資訊添加功能
try{
file://僅當頁面是有效時候才處理它
if(Page.IsValid){

errmess.Text="" ;
file://以讀的模式開啟一個FileStream來訪問資料庫
FileStream fin;
fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,
FileAccess.Read,FileShare.ReadWrite);
file://建立一個資料庫物件
DataSet guestData = new DataSet();
file://僅從資料庫讀取XML Schema
guestData.ReadXmlSchema(fin);
fin.Close();
file://從資料集的Schema建立一個資料行
DataRow newRow = guestData.Tables[0].NewRow();
file://用相應值填寫資料行
newRow["Name"]=Name.Text;
newRow["Country"]=Country.Text;
newRow["Email"]=Email.Text;
newRow["Comments"]=Comments.Text;
newRow["DateTime"]=DateTime.Now.ToString();
file://填寫完畢,將資料行添加到資料集
guestData.Tables[0].Rows.Add(newRow);
file://為資料庫檔案建立另一個寫入模式的FileStream,並儲存檔案
FileStream fout ;
fout = new FileStream(Server.MapPath(dataFile),FileMode.Open,
FileAccess.Write,FileShare.ReadWrite);

guestData.WriteXml(fout, XmlWriteMode.WriteSchema);
fout.Close();
file://隱藏當前的面板
formPanel.Visible=false;
file://顯示帶有感謝函息的面板
thankPanel.Visible=true;
}
}
catch (Exception edd)
{
file://捕捉異常
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"
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 >



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.