打印本文 打印本文  关闭窗口 关闭窗口
一个ASP.NET+XML留言本例子
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:02:33
viewpost.aspx--察看提交的留言
viewguestbook.aspx--察看所有留言
Header.inc
guestpost.aspx--留言表单及XML写操作
Footer.inc
Guest.xml--XML数据

源代码如下:

viewguestbook.aspx
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C<%-- Needed Assembiles --%>

<html>
<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script language="C //run the script when the Page is Loaded
public void Page_Load(Object sender, EventArgs e)
{
// an label , its use stated later
tryagain :

//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 = "db/guest.xml" ;

//try-Catch block to read from an XML file
try
{
//make an instance to the XMLDataDocument class
//this class can read from an xml file in and ordered format
XmlDataDocument datadoc = new XmlDataDocument();

// Infer the DataSet schema from the XML data and load the XML Data
datadoc.DataSet.ReadXml(new StreamReader(Server.MapPath(datafile)));

//Databind the first table in the Dataset to the Repeter
MyDataList.DataSource = datadoc.DataSet.Tables[0].DefaultView;
MyDataList.DataBind();

//free up the XML file to be used by other programs
datadoc=null;


}
catch(IOException ed)
{
// Here I am for now trying to overcome a bug in my guestbook exapmle
//the Bug is that only one class can either read or write to my XML
// data file at a time.
//If the file is being used my some some other page (eg the guest book viewing page)
// then an IOException will be thrown
// So to handle such situtations what we do is that
// If an IOException is thrown the page goes again to the tryagain label
//and tries to write again into the xml file
//this goes on till finally the resource is freed and the xml file is written to.

goto tryagain ;
}
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">
<!-- <asp:label id="errmess" text="" style="color:<br>
<h3 align="center" class="newsbody">My Guestbook.</h3>
<ASP:Repeater id="MyDataList" runat="server">

<template name="headertemplate">

<table class="mainheads" width="100%" style="font: 8pt verdana">
<tr style="background-color: <th>
Name
</th>
<th>
Country
</th>
<th>
Email
</th>
<th>
Comments
</th>
<th>
Date/Time
</th>
</tr>

</template>

<template name="itemtemplate">

<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" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<!-- <asp:label id="errmess" text="" style="color: <center>
<h2 class="newsbody"><b>Thank You , for posting in My GuestBook.</b></h2>
<table align=center width="60%" border="0" cellspacing="2" cellpadding="1" >
<tr class="titheading"><td colspan="2">The information You Posted!</td></tr>
<tr class="newsbody">
<td>Name :</td>
<td><asp:label id="NameLabel" text="" runat="server" /></td>
</tr>
<tr class="newsbody">
<td>Country :</td>
<td><asp:label id="CountryLabel" text="" runat="server" /></td>
</tr>
<tr class="newsbody">
<td>E-mail :</td>
<td><asp:label id="EmailLabel" text="" runat="server"/></td>
</tr>
<tr class="newsbody">
<td>Comments :</td>
<td><asp:label id="CommentsLabel" text="" runat="server" /></td>
</tr>
</table>
<br>
<h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4><br>
</center>
<!--
</body>
</html>


guestpost.aspx

<%@ Import Namespace="System" %>
<%@ Page Language="C<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%-- These are the imported assembiles and namespaces need to run the guest book --%>

<html>

<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script Language="C ///<summary>
/// This methord is called when the submit button is clicked
///</summary>
public void Submit_Click(Object sender, EventArgs e)
{
//A label need to the goto statement explained below
tryagain :

//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 = "db/guest.xml" ;

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

//proceed only if all the required feilds are filled-in
if(Page.IsValid&&Name.Text!=""&&Country.Text!=""&&Email.Text!=""){

errmess.Text="" ;

//make an instance of the class XmlDocument
XmlDocument xmldocument = new XmlDocument() ;

//load the xml file you will use as your database.
//Since we are working on a server we have to use 'Server.MapPath()'
//to mape the path to the database file

xmldocument.Load(Server.MapPath(datafile)) ;

//make an instance of DocumentNavigator class which will help us to
//navigate in the loaded XML data file.
DocumentNavigator navigator = new DocumentNavigator(xmldocument) ;

//below code is very significant as it navigates through the XML document and
//stores the required values (ps: read this code carefully)

//first move to the xml documents elements (in my xml file this will come to the 'Guests' Node
navigator.MoveToDocumentElement() ;

//then insert First element (FirstChild) which will contain all the information
// of a single guest posting
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Guest","","") ;

//Insert the Element of Name as the First node of 'Guest'
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Name","","") ;
//This is important to specify what kind of Value will the Name element contain
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Name","","") ;
//assign the Name element the Value from the .Text property of the TextBox
navigator.Value=Name.Text ;

//Go back to the Parent Node ie 'Guest'
navigator.MoveToParent() ;

//Insert another Element 'Country' After the FirstChild ie. after the 'Name' node.
navigator.Insert(TreePosition.After, XmlNodeType.Element,"Country","","") ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Country","","") ;
naviga

[1] [2]  下一页



打印本文 打印本文  关闭窗口 关闭窗口