?? guestpost.aspx
字號:
<%--
guestpost.aspx
這個是留言簿主文件,展示了如何打開和寫入一個XML文件從一個WEB表單取得數(shù)據(jù)。
--%>
<%@ Page Language="C#" EnableSessionState="False" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%-- 上面的是留言簿正常運(yùn)行需要引入的命名空間 --%>
<html>
<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script Language="C#" runat="server">
//當(dāng)submit按鈕單擊的時候觸發(fā)該方法
public void Submit_Click(Object sender, EventArgs e)
{
//該路徑下的 Xml 文件包含了所有的數(shù)據(jù)
string dataFile = "db/guest.xml" ;
//處理提交數(shù)據(jù)在 Try-Catch 模塊
try{
//如果頁面有效,繼續(xù)執(zhí)行
if(Page.IsValid){
errmess.Text="" ;
//打開一個 FileStream 為了數(shù)據(jù)庫的"讀"模式
FileStream fin;
fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
//生成一個 DataSet 對象
DataSet guestData = new DataSet();
//從數(shù)據(jù)庫中讀出數(shù)據(jù)
guestData.ReadXml(fin);
fin.Close();
//生成一個新的數(shù)據(jù)行
DataRow newRow = guestData.Tables[0].NewRow();
//使用表單提交值值填充數(shù)據(jù)行
newRow["Name"]=Name.Text;
newRow["Country"]=Country.Text;
newRow["Email"]=Email.Text;
newRow["Comments"]=Comments.Text;
newRow["DateTime"]=DateTime.Now.ToString();
//增加行到 DataSet
guestData.Tables[0].Rows.Add(newRow);
//為操作數(shù)據(jù)庫,生成另外一個 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)
{
//產(chǎn)生任何例外的時候出現(xiàn)
errmess.Text="不能寫入XML文件,因?yàn)?"+edd.ToString() ;
}
}
</script>
<LINK href="mystyle.css" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<%-- 包含頭文件 '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>內(nèi)容 :</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="提交" 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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -