Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Create Your Own Guestbook In ASP.NET


Posted Date: 24 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: Placement Papers

Posted By: ramya       Member Level: Gold
Rating:     Points: 5





Creating your very own guestbook is easy with help from Sonu. Using ASP.NET with technologies like XML/XSL, you can have your guestbook up in no time. Read more ...

Recently I was working on my website in which I wanted to implement a guestbook. So I searched the web to get the best guestbook for my Website. But then I thought: Hey I am a developer, why not create my own one. It was very easy to create a guestbook and you can do it too. In this article I will show you how you can easily create a guestbook. To understand the article, I assume that you have already knowledge about the basics of ASP.NET programming and XML/XSL skills.

Overview

So what do we need to create a guestbook. We need two webforms, one to enter the name, email, comment etc. and the other is used to display the comments signed in the guestbook. Of course we can make this in one webform, but to have a clean code, I will use two webforms with several codebehind files, which I discuss later.

Then we need a database, which holds the information for us. I have used a simple XML file ( database ) to store the information entered by the user. For the visualisation of the XML we use also the XSL technique. In summary we need the following:

- Two webforms
- Codebehind
- Database
- XSL

the guestbook.aspx, you should see a webform like this:

By now you have seen how to display a webform, but you have not seen the code yet that is handling the event in guestbooks.cs.

The Codebehind - guestbook.cs

01: using System;
02: using System.Web;
03: using System.Web.UI;
04: using System.Web.UI.WebControls;
05: using System.Xml;
06: public class Guestbook : Page
07: {
08: // Create the required webcontrols with the same name as in the guestbook.aspx 09: //file.
10: public TextBox name;
11: public TextBox location;
12: public TextBox email;
13: public TextBox website;
14: public TextBox comment;
15: public void Save_Comment(object sender, EventArgs e)
16: {
17: // Everything is all right, so let us save the data into the XML file
18: SaveXMLData();
19: // Remove the values of the textboxes
20: name.Text="";
21: location.Text="";
22: website.Text="";
23: email.Text="";
24: comment.Text="";
25: }
26: }
27: private void SaveXMLData()
28: {
29: // Load the xml file
30: XmlDocument xmldoc = new XmlDocument();
31: xmldoc.Load( Server.MapPath("guestbook.xml") );
32: //Create a new guest element and add it to the root node
33: XmlElement parentNode = xmldoc.CreateElement("guest");
34: xmldoc.DocumentElement.PrependChild(parentNode);
35: // Create the required nodes
36: XmlElement nameNode = xmldoc.CreateElement("name");
37: XmlElement locationNode = xmldoc.CreateElement("location");
38: XmlElement emailNode = xmldoc.CreateElement("email");
39: XmlElement websiteNode = xmldoc.CreateElement("website");
40: XmlElement commentNode = xmldoc.CreateElement("comment");
41: // retrieve the text
42: XmlText nameText = xmldoc.CreateTextNode(name.Text);
43: XmlText locationText = xmldoc.CreateTextNode(location.Text);
44: XmlText emailText = xmldoc.CreateTextNode(email.Text);
45: XmlText websiteText = xmldoc.CreateTextNode(website.Text);
46: XmlText commentText = xmldoc.CreateTextNode(comment.Text);
47: // append the nodes to the parentNode without the value
48: parentNode.AppendChild(nameNode);
49: parentNode.AppendChild(locationNode);
50: parentNode.AppendChild(emailNode);
51: parentNode.AppendChild(websiteNode);
52: parentNode.AppendChild(commentNode);
53: // save the value of the fields into the nodes
54: nameNode.AppendChild(nameText);
55: locationNode.AppendChild(locationText);
56: emailNode.AppendChild(emailText);
57: websiteNode.AppendChild(websiteText);
58: commentNode.AppendChild(commentText);
59: // Save to the XML file
60: xmldoc.Save( Server.MapPath("guestbook.xml") );
61: // Display the user the signed guestbook
62: Response.Redirect("viewguestbook.aspx");
63: }
64: }

So far concerning the codebehind file, but what really happens here ?

You won't believe it, but not much. In line 1 to 5, I have implemented the minimal required namespaces which are needed to get access to several functions. In line 6, I have created a new class called Guestbook, please notice this is the class which is inherited by the guestbook.aspx file.

The line 10 to 14, declares 5 public variables of type type textbox, please remember also here, that these names have to be identical with the textboxes created in guestbook.aspx. In line 15, you can see the event Save_Comment, which is fired by the submit button of the guestbook.aspx file. This event is used to save the data.

The Saving Process

The function SaveXMLData() does the saving of the information for us. As we are using a XML database to store the information, we use the XmlDocument, XmlElement and XmlText classes. This classes provides the necassary functions which we need.

The lines 30 and 31 creates a new XMLDocument class object and loads the guestbook.xml file. In lines 36 to 40, the required nodes are created with the function CreateElement. The lines 42 to 46 retrieve the informations entered by the user and store them to an object of XmlText.

In lines 48 to 52, I have used the function AppendChild with the main XmlDocument object. This function stores the created nodes without the values. Finally in lines 54-88 the values are stored in the nodes we just created. In line 60, all changes are saved to the guestbook.xml. Line 62 redirects the page to the viewguestbook.aspx, to display the stored comment.

The Webforms - Part II - Viewing the Guestbook

To view the guestbook, I have created an another webform. Take a look at the second webform.

ViewGuestbook.aspx

01: <% @Page Language="C#" Debug="true" Src="ViewGuestbook.cs" Inherits="ViewGuestbook" %>

As you see I am not doing very much in the webform. I have just called the codebehind file ViewGuestbook.cs. So please take a look at this file.

The Codebehind - ViewGuestbook.cs

01: using System;
02: using System.Web;
03: using System.Web.UI;
04: using System.Web.UI.WebControls;
05: using System.Xml;
06: using System.Xml.Xsl;
07: using System.IO;
08: public class ViewGuestbook : Page
09: {
10: private void Page_Load(object sender, System.EventArgs e)
11: {
12: //Load the XML file
13: XmlDocument doc = new XmlDocument( );
14: doc.Load( Server.MapPath("guestbook.xml") );
15: //Load the XSL file
16: XslTransform xslt = new XslTransform();
17: xslt.Load( Server.MapPath("guestbook.xsl") );
18: string xmlQuery="//guestbook";
19: XmlNodeList nodeList=doc.DocumentElement.SelectNodes(xmlQuery);
20: MemoryStream ms=new MemoryStream();
21: xslt.Transform( doc, null, ms);
22: ms.Seek( 0, SeekOrigin.Begin );
23: StreamReader sr = new StreamReader(ms);
24: //Print out the result
25: Response.Write(sr.ReadToEnd());
26: }
27:}

I have created this class to display all comments to the user. Lines 1-7 are again used to implement the required namespaces. As we are using XSL for the visualisation we have to include the namespace System.Xml.Xsl. Line 8 creates a new class called ViewGuestbook, with a private inbuilt function called Page_Load.

This function is always called when the page loads or when the user performs a refresh. The function loads again the guestbook.xml in line 14. The XslTranform class is used to transform the XML elements into HTML. In line 16 to 17, I am loading the guestbook.xsl with the help of a XslTransform object. Line 19 creates a new object of class XmlNodeList.

With the help of this class we can select the required nodes. In line 20, I have used the class MemoryStream, which is avalable via the namespace System.IO. This class is used to create a stream that has memory as a backing store. With the function Transform in line 21, I have assigned the xml data to the memorystream. The function Seek in line 22, sets the current position to zero. In line 23, I have created an object of the class StreamReader. This class is used to read the stream.

Line 25 then prints the result with the help of the function ReadToEnd(). This function reads the stream from the current position to the end. If you run the viewguestbook.aspx, you should see a webform like this:


As already mentioned, we use XSL for the transformation from XML to HTML. I assume that you already have knowledge about XSLT, so I will only discuss the important things. I have only used a xsl for-each loop to iterate through the all guests. This looks something like this:

01:
02:
03:


In the loop I am calling the xsl template name, which looks something like this:

01:
02:
03:


CONCLUSION:
As you see it is not very difficult to create a guestbook.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Introducing C# and the .NET Framework
Previous Resource: VB.NET INTERVIEW QUESTIONS
Return to Discussion Resource Index
Post New Resource
Category: Placement Papers


Post resources and earn money!
 
Related Resources



Watch TV Channels
  • Watch Asianet TV online
  • Kairali TV in Internet
  • Surya TV online
  • Amritha TV Channel

  • Contact Us    Privacy Policy    Terms Of Use   

    SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.