一个ASP.NET+XML留言本例子

viewpost.aspx--察看提交的留言
viewguestbook.aspx--察看所有留言
Header.inc
guestpost.aspx--留言表单及XML写操作
Footer.inc
Guest.xml--XML数据

源代码如下:

viewguestbook.aspx

1@ Import Namespace="System" 
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Data" 
1@ Assembly Name="System.Xml" 
1@ Import Namespace="System.Xml" 
1@ Page Language="C<%-- Needed Assembiles --
 1<html>
 2<head>
 3<title>Welcome to Saurabh's GuestBook.</title>
 4<script (eg="" (exception="" +edd.tostring()="" ;="" <="" a="" again="" am="" an="" and="" any="" at="" be="" being="" block="" book="" bug="" by="" can="" catch="" catch(ioexception="" class="" data="" databind="" datadoc="null;" datadoc.dataset.readxml(new="" dataset="" db="" do="" ed)="" edd)="" either="" errmess.text="Cannot read from XML file because " exapmle="" exceptions="" file="" finally="" first="" for="" format="" free="" freed="" from="" goes="" goto="" guest="" guest.xml"="" guestbook="" handle="" here="" i="" if="" in="" infer="" instance="" into="" ioexception="" is="" label="" language="C //run the script when the Page is Loaded   
 5public void Page_Load(Object sender, EventArgs e)   
 6{   
 7// an label , its use stated later   
 8tryagain :   
 9  
10//the path to the Xml file which will contain all the data   
11//modify this if you have any other file or directory mappings.   
12//modify this if you have been directed here from Step 2 of the ReadMe file.   
13string datafile = " load="" make="" my="" mydatalist.databind();="" mydatalist.datasource="datadoc.DataSet.Tables[0].DefaultView;" now="" occur="" on="" one="" only="" or="" ordered="" other="" overcome="" page="" page)="" programs="" read="" repeter="" resource="" schema="" script="" situtations="" so="" some="" streamreader(server.mappath(datafile)));="" such="" table="" that="" the="" then="" this="" thrown="" till="" time.="" to="" to.="" tries="" try="" try-catch="" tryagain="" trying="" up="" used="" viewing="" we="" what="" will="" write="" written="" xml="" xmldatadocument="" xmldatadocument();="" {="" }="">   
14<LINK href="mystyle.css" type=text/css rel=stylesheet>   
15  
16</head>   
17<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" rightmargin="0">   
18<!-- <asp:label id="errmess" text="" style="color:<br>   
19<h3 align="center" class="newsbody">My Guestbook.</h3>   
20<ASP:Repeater id="MyDataList" runat="server">   
21  
22<template name="headertemplate">   
23  
24<table class="mainheads" width="100%" style="font: 8pt verdana">   
25<tr style="background-color: <th>   
26Name   
27</th>   
28<th>   
29Country   
30</th>   
31<th>   
32Email   
33</th>   
34<th>   
35Comments   
36</th>   
37<th>   
38Date/Time   
39</th>   
40</tr>   
41  
42</template>   
43  
44<template name="itemtemplate">   
45  
46<tr style="background-color: <td>   

</td>
<td>
<% </td>
<td>
<% </td>
<td>
<% </td>
<td>
<% </td>
</tr>

</template>

<template name="footertemplate">

</table>

</template>

</ASP:Repeater>

<!-- </body>
</html>

viewpost.aspx
<%@ Import Namespace="System"

@ Page Language="C<html>
<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script language="C //execute this script when the page loads
void Page_Load(Object Src, EventArgs E)
{
//if the page is called from anothe page
if (!Page.IsPostBack) {
//get the diffrent Parameters from the querry string and store it
//to respective Labels
NameLabel.Text = Request.Params["Name"];
CountryLabel.Text= Request.Params["Country"] ;
EmailLabel.Text=Request.Params["Email"];
CommentsLabel.Text=Request.Params["Comments"] ;
}
if(Page.IsPostBack)
{
//else display an error
errmess.Text="This Page Cannot be called directly. It has to be called from the Guestbook posting page." ;
}
}
</script> <link href="mystyle.css" rel="stylesheet" type="text/css"/> </head> <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
&lt;!-- &lt;asp:label id="errmess" text="" style="color: &lt;center&gt;
<h2 class="newsbody"><b>Thank You , for posting in My GuestBook.</b></h2> <table align="center" border="0" cellpadding="1" cellspacing="2" width="60%"> <tr class="titheading"><td colspan="2">The information You Posted!</td></tr> <tr class="newsbody"> <td>Name :</td> <td><asp:label id="NameLabel" runat="server" text=""></asp:label></td> </tr> <tr class="newsbody"> <td>Country :</td> <td><asp:label id="CountryLabel" runat="server" text=""></asp:label></td> </tr> <tr class="newsbody"> <td>E-mail :</td> <td><asp:label id="EmailLabel" runat="server" text=""></asp:label></td> </tr> <tr class="newsbody"> <td>Comments :</td> <td><asp:label id="CommentsLabel" runat="server" text=""></asp:label></td> </tr> </table> <br/> <h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4><br/>

&lt;!--
&lt;/body&gt;
</body></html>

1guestpost.aspx   
2  
3<%@ Import Namespace="System" 
1@ Page Language="C<%@ Import Namespace="System.IO" 
1@ Assembly Name="System.Xml" 
1@ Import Namespace="System.Xml" 
1-- These are the imported assembiles and namespaces need to run the guest book --
 1<html>
 2<head>
 3<title>Welcome to Saurabh's GuestBook.</title>
 4<script "+now.toshorttimestring()="" "guest","","")="" "name","","")="" &&country.text!="" &&email.text!="" 'country'="" 'guest'="" 'guests'="" 'name'="" 'server.mappath()'="" (eg="" (exception="" (firstchild)="" (in="" (ps:="" ){="" +="" +edd.tostring()="" .text="" ;="" <="" a="" after="" again="" all="" am="" an="" and="" another="" any="" append="" are="" as="" assign="" at="" back="" be="" being="" below="" block="" book="" bug="" build="" can="" carefully)="" catch="" catch(ioexception="" changes="" chenges="" class="" code="" come="" contain="" custom="" data="" database="" database.="" date="" datetime="" db="" display="" do="" document="" documentnavigator="" documentnavigator(xmldocument)="" documents="" ed)="" edd)="" either="" element="" elements="" else="" empty="" encode="" end.="" entry="" errmess.text="Cannot write to XML file because " error="" exapmle="" exception="" feilds="" file="" file.="" filled-in="" finally="" first="" firstchild="" for="" format="" free="" freed="" from="" go="" goes="" goto="" guest="" guest.xml"="" guestbook="" handle="" have="" help="" here="" i="" ie="" ie.="" if="" if(page.isvalid&&name.text!="" important="" in="" information="" insert="" instance="" into="" ioexception="" is="" it="" its="" kept="" kind="" label="" language="C ///&lt;summary&gt;   
 5/// This methord is called when the submit button is clicked   
 6///&lt;/summary&gt;   
 7public void Submit_Click(Object sender, EventArgs e)   
 8{   
 9//A label need to the goto statement explained below   
10tryagain :   
11  
12//the path to the Xml file which will contain all the data   
13//modify this if you have any other file or directory mappings.   
14//modify this if you have been directed here from Step 2 of the ReadMe file.   
15string datafile = " load="" loaded="" make="" making="" mape="" message="" move="" my="" name="" navigate="" navigates="" navigator="new" navigator.insert(treeposition.after,="" navigator.insert(treeposition.after,xmlnodetype.element,"comments","","")="" navigator.insert(treeposition.after,xmlnodetype.element,"email","","")="" navigator.insert(treeposition.firstchild,="" navigator.movetodocumentelement()="" navigator.movetoparent()="" navigator.value='now.ToShortDateString()+"' necessary="" node="" node.="" now="" occur="" of="" on="" one="" only="" or="" other="" overcome="" page="" page)="" page.navigate("viewpost.aspx?"="" parent="" path="" posted="" posting="" proceed="" programs="" property="" put="" querry="" querystring="" querystring);="" read="" required="" resource="" save="" script="" sending="" server="" set="" show="" significant="" since="" single="" situtations="" so="" some="" specify="" stamp="" stores="" string="" such="" system.text.encoding.utf8);="" system.web.httputility.urlencodetostring(comments.value,="" system.web.httputility.urlencodetostring(country.text,="" system.web.httputility.urlencodetostring(email.text,="" system.web.httputility.urlencodetostring(name.text,="" textbox="" that="" the="" then="" this="" through="" thrown="" till="" time="" time.="" to="" to.="" tries="" try-catch="" tryagain="" trying="" try{="" up="" us="" use="" used="" utf8="" value="" values="" very="" viewing="" viewpost.aspx="" we="" what="" which="" will="" within="" working="" write="" written="" xml="" xmldocument="null" xmldocument()="" xmldocument.load(server.mappath(datafile))="" xmldocument.save(server.mappath(datafile))="" xmlnodetype.element,="" xmlnodetype.element,"country","","")="" xmlnodetype.element,"datetime","","")="" xmlnodetype.text,"comments","","")="" xmlnodetype.text,"country","","")="" xmlnodetype.text,"datetime","","")="" xmlnodetype.text,"email","","")="" xmlnodetype.text,"name","","")="" you="" your="" {="" }=""></script></head></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus