您的位置:首页 > 其它

[.net]早期的基于XML的简易留言本

2004-08-12 11:29 381 查看
由6个文件组成,核心文件3个,达成简易的添加留言(guestpost.aspx),查看留言(viewguestbook.aspx)

guestpost.aspx
============

<%@ Page Language="C#" EnableSessionState="False" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%-- These are the imported namespaces needed to run the guest book --%>

<html>

<head>
<title>Toll's GuestBook.</title>
<script Language="C#" runat="server">
///<summary>
/// This method is called when the submit button is clicked
///</summary>
public void Submit_Click(Object sender, EventArgs e)
{

//the path to the Xml file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string dataFile = "guest.xml" ;

//put the posting code within a Try-Catch block
try{

//proceed only if the page is Valid
if(Page.IsValid){

errmess.Text="" ;
//Open a FileStream to the Database in read mode
FileStream fin;
fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
//Create a DataSet object
DataSet guestData = new DataSet();
//Read data from the Database
guestData.ReadXml(fin);
fin.Close();
//Create a new DataRow from the DataSet Schema
DataRow newRow = guestData.Tables[0].NewRow();
//Fill the DataRow with form values
newRow["Name"]=Name.Text;
newRow["Country"]=Country.Text;
newRow["Email"]=Email.Text;
newRow["Comments"]=Comments.Text;
newRow["DateTime"]=DateTime.Now.ToString();
//Add the row to the DataSet
guestData.Tables[0].Rows.Add(newRow);
//Create another filestream to the DataBase file
//in write mode and save the file!
FileStream fout ;
fout = new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Write,FileShare.ReadWrite);

guestData.WriteXml(fout, XmlWriteMode.WriteSchema);
fout.Close();
//Hide the Form Panel
formPanel.Visible=false;
//Display the Thank Panel
thankPanel.Visible=true;
}
}
catch (Exception edd)
{
//catch any other exception that occur
errmess.Text="Cannot write to XML file because "+edd.ToString() ;

}
}
</script>
<LINK href="mystyle.css" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<%-- Include a header file 'header.inc' --%>
<!-- #Include File="header.inc" -->
<br>
<h3 align="center" >Post Message</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>Sign-in My GuestBook</b></td>
<td class="newsheading"> </td>

</tr>
<tr class="newsbody" >
<td>Name :</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>Country :</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">Please enter a valid e-mail address</asp:RegularExpressionValidator></td>

</tr>
<tr class="newsbody">
<td>Comments :</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>Thank you for posting in my Guestbook!</b><br>
<a href="viewguestbook.aspx">Click here</a> to view GuestBook.
</p>
</asp:Panel>

<!-- #Include File="footer.inc" -->
</body>
</html>

viewguestbook.aspx
==============

<%@ Page Language="C#"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<title>Toll's GuestBook.</title>
<script language="C#" runat=server>
//run the script when the Page is Loaded

public void Page_Load(Object obj, EventArgs e)
{

//the path to the Xml file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile = "guest.xml" ;

//try-Catch block to read from an XML file
try
{
//create a DataSet object
DataSet guestData = new DataSet();
//Open a FileStream to the Database
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ;
//Read the Database into the DataSet
guestData.ReadXml(new StreamReader(fin));
fin.Close();
//Databind the first table in the Dataset to the Repeater
MyDataGrid.DataSource = guestData.Tables[0].DefaultView;
MyDataGrid.DataBind();
}
catch (Exception edd)
{
//catch any other exceptions that occur
errmess.Text="Cannot read from XML file because "+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" />
<ASP:Repeater id="MyDataGrid" runat="server">

<headertemplate>

<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr style="background-color:#cccccc" height="28">
<th>
Name
</th>
<th>
Country
</th>
<th>
Email
</th>
<th>
Messages
</th>
<th>
Date/Time
</th>
</tr>

</headertemplate>

<itemtemplate>

<tr style="background-color:#eeeeef">
<td>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
</td>
<td>
<%# "<a href=mailto:" +DataBinder.Eval(Container.DataItem, "Email") + ">" + DataBinder.Eval(Container.DataItem, "Email") + "</a>"%>
</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>

guest.xml
=========

<Guests>
<xs:schema id="Guests" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Guests" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Guest">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Country" type="xs:string" minOccurs="0" />
<xs:element name="Email" type="xs:string" minOccurs="0" />
<xs:element name="Comments" type="xs:string" minOccurs="0" />
<xs:element name="DateTime" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Guest>
<Name>hello</Name>
<Country>China</Country>
<Email>toll2@sacrm.com</Email>
<Comments>test system</Comments>
<DateTime>2003-12-31 14:36:21</DateTime>
</Guest>
<Guest>
<Name>admin</Name>
<Country>China</Country>
<Email>arthur@sacrm.com</Email>
<Comments>welcome</Comments>
<DateTime>2003-12-31 14:38:03</DateTime>
</Guest>
<Guest>
<Name>sohu</Name>
<Country>China</Country>
<Email>haha@haeeeha.com</Email>
<Comments>this is test

multiline

so</Comments>
<DateTime>2003-12-31 16:19:35</DateTime>
</Guest>
<Guest>
<Name>newye</Name>
<Country>China</Country>
<Email>newlu@arkcrm.com</Email>
<Comments>is finished</Comments>
<DateTime>2004-3-2 9:26:05</DateTime>
</Guest>
</Guests>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: