Four Code:
(1) Guestpost.aspx:
<%@ Page language= "C #" enablesessionstate= "False"% >
<%@ Import namespace= "System"% >
<%@ Import namespace= "System.IO"% >
<%@ Import namespace= "System.Data"% >
<%--These are the name spaces necessary for the normal use of this procedure--% >
< HTML >
< head >
< title > Welcome to my Guest Book </title >
< script language= "C #" runat= "Server" >
< summary >
When the submit button is pressed, adjust the function
</summary >
public void Submit_click (Object sender, EventArgs e)
{
file:// The path of the XML file where the data is stored
file://If your path is different from the following, please modify it
string datafile = "Db/guest.xml";
file:// with a try-catch block to complete the information add function
try{
file://only deal with pages when they are valid
if (page.isvalid) {
Errmess. Text= "";
file://toRead mode opens a FileStream to access the database
FileStream fin;
fin= New FileStream (Server.MapPath (datafile), FileMode.Open,
Fileaccess.read,fileshare.readwrite);
file://builtEstablish a Database object
DataSet Guestdata = new DataSet ();
file://onlyReading an XML Schema from a database
Guestdata.readxmlschema (Fin);
Fin. Close ();
file://fromSchema of the dataset create a new data row
DataRow NewRow = guestdata.tables[0]. NewRow ();
file://withFill data rows with corresponding values
newrow["Name"]=name.text;
newrow["Country"]=country.text;
newrow["Email"]=email.text;
newrow["Comments"]=comments.text;
newrow["DateTime"]=datetime.now.tostring ();
file://FillFinished writing, adding data rows to the dataset
Guestdata.tables[0]. Rows.Add (NewRow);
file://forThe database file creates a new FileStream for another write mode and saves the file
FileStream Fout;
Fout = new FileStream (Server.MapPath (datafile), FileMode.Open,
Fileaccess.write,fileshare.readwrite);
Guestdata.writexml (Fout, Xmlwritemode.writeschema);
Fout. Close ();
file:// hides the current panel
Formpanel.visible=false;
file://display a panel with a thank-you message
Thankpanel.visible=true;
}
}
catch (Exception edd)
{
file://catching and catching anomaly
Errmess. text= "Error writing XML file, Reason:" +edd. ToString ();
}
}
</script >
< LINK href= "mystyle.css" Type=text/css rel=stylesheet >
< BODY >
<%--contains a header file: Header.inc--% >
<!--#Include file= "Header.inc"-->
< br >
< h3 align= "center" class= "newsbody" > Message information </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 > Please leave your valuable information 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 > National:</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 properly formatted email address! </asp:regularexpressionvalidator ></td >
</tr >< tr class= "Newsbody" >< td > Message:</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 > Thank you for visiting my guest book! </b >
< br >< a href= "viewguestbook.aspx" > Please click here </a > to view the guest book.
</p >
</asp:panel >
<!--#Include file= "Footer.inc"-->
</body >