由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 /></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 >

      <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 />
              </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>

相关文章:

  • 2021-12-19
  • 2022-12-23
  • 2021-06-07
  • 2021-11-28
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-24
  • 2021-10-06
  • 2021-10-15
  • 2021-11-05
  • 2021-05-10
  • 2021-10-04
  • 2022-02-08
相关资源
相似解决方案