Using ASP.net to implement XML-based guest Book Two

Source: Internet
Author: User

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 >



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.