Article

Create Your Own Guestbook In ASP.NET

Page: 1 2 3 Next

Now we know how to display the Web form, but we haven't seen the code that handles the event in guestbooks.cs. Let's take a look at that now.

using System;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Xml;  
 
public class Guestbook : Page  
{  
// Create the required webcontrols with the same name as  
in the guestbook.aspx file  
 public TextBox name;  
 public TextBox location;  
 public TextBox email;  
 public TextBox website;  
 public TextBox comment;  
 
 public void Save_Comment(object sender, EventArgs e)  
 {  
  // Everything is all right, so let us save the data  
into the XML file  
  SaveXMLData();  
 
  // Remove the values of the textboxes  
  name.Text="";  
  location.Text="";  
  website.Text="";  
  email.Text="";  
  comment.Text="";  
 }  
}  
 
private void SaveXMLData()  
{  
 // Load the xml file  
 XmlDocument xmldoc = new XmlDocument();  
 xmldoc.Load( Server.MapPath("guestbook.xml") );  
 
 //Create a new guest element and add it to the root node  
 XmlElement parentNode = xmldoc.CreateElement("guest");  
 xmldoc.DocumentElement.PrependChild(parentNode);  
 
 // Create the required nodes  
 XmlElement nameNode = xmldoc.CreateElement("name");  
 XmlElement locationNode = xmldoc.CreateElement("location");  
 XmlElement emailNode = xmldoc.CreateElement("email");  
 XmlElement websiteNode = xmldoc.CreateElement("website");  
 XmlElement commentNode = xmldoc.CreateElement("comment");  
 
 // retrieve the text  
 XmlText nameText = xmldoc.CreateTextNode(name.Text);  
 XmlText locationText = xmldoc.CreateTextNode(location.Text);  
 XmlText emailText = xmldoc.CreateTextNode(email.Text);  
 XmlText websiteText = xmldoc.CreateTextNode(website.Text);  
 XmlText commentText = xmldoc.CreateTextNode(comment.Text);  
 
 // append the nodes to the parentNode without the value  
 parentNode.AppendChild(nameNode);  
 parentNode.AppendChild(locationNode);  
 parentNode.AppendChild(emailNode);  
 parentNode.AppendChild(websiteNode);  
 parentNode.AppendChild(commentNode);  
 
 // save the value of the fields into the nodes  
 nameNode.AppendChild(nameText);  
 locationNode.AppendChild(locationText);  
 emailNode.AppendChild(emailText);  
 websiteNode.AppendChild(websiteText);  
 commentNode.AppendChild(commentText);  
 
 // Save to the XML file  
 xmldoc.Save( Server.MapPath("guestbook.xml") );  
 
 // Display the user the signed guestbook  
 Response.Redirect("viewguestbook.aspx");  
 }  
}

Wow! That's our codebehind file... but what really happens here? You won't believe it, but the answer is: "not much"!

First, we implement the minimal required namespaces which we need in order to access several important functions. Then I create a new class called Guestbook:

public class Guestbook : Page

Note that it's this class that's inherited by the guestbook.aspx file. Then we declare 5 public variables of type textbox. Remember that here, the names have to be identical to those we used when we created the text boxes in guestbook.aspx. Then, as you can see, we use the Save_Comment event, which is fired by the submit button we included in the guestbookpx file. This event is used to save the data.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links