Quantcast

Your Own Guestbook In ASP.NET

Get the WebProNews Newsletter:
[ Business]

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

In a guestbook it is usually fully sufficient to store the name, location, email, website and some lines of comment. Of course you can have more fields to store, but I think these are enough. This data is stored in the XML file. So according to that our XML can look something like this:

Guestbook.xml:

-==-

The webforms – Part I – Signing the guestbook:

To sign a guestbook we allow the user to enter some information. This can be done in a simple webform. In our example this the guestbook.aspx file. I use the following fields in the webform which can be filled by the user.

- Name
- Location
- Email
- Website
- Comment

The first Webform – guestbook.aspx:

-==-

To avoid confusing you with unnecessary code, I have removed the visualisation tags – like table, table header etc. from this example. Of course these are all included in the example download. As we only display a form with some fields and buttons, you don’t see any real programming code. This is all hidden in the codebehind. I assume you already know the technique of codebehind. In line 1 I have set the SRC attribute to let the asp.net file know that we are using the codebehind file Guestbook.cs and I have set also the attribute Inherits with the corresponding classname. This attribute is used to let the file know, which class has to be inherited. In lines 6, 12, and 18 to 20 I have implemented the required textfields. Please remember that if you want to use the same variables in codebehind, they need to have the same ID in both files and must be declared as public. In the lines 8, 14, and 22 I have used the ASP.NET validator controls. This validator controls checks whether the user has entered any value in the textfields, without doing any round-trip to the server. This code is executed on the client side. In line 26, I have implemented a submit button with a OnClick event called Save_Comment. This event is used to store the informations entered by the user to the XML file. The function of this event is available in the Guestbook.cs. In line 27 I have only implemented a reset button. This is all and nothing more has to be done in the webform. If you run 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:

-==-

So far concerning the codebehind file, but what really happens here ? You won’t believe it, but not much. In line 1-3 I have implemented the minimal required namespaces which are needed to get access to several functions. In line 7 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-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 16 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 33-34 creates a new XMLDocument class object and loads the guestbook.xml file. In lines 41-45 the required nodes are created with the function CreateElement. The lines 48-52 retrieve the informations entered by the user and store them to an object of XmlText. In lines 55-59, I have used the function AppendChild with the main XmlDocument object. This function stores the created nodes without the values. Finally in lines 62-68 the values are stored in the nodes we just created. In line 69 all changes are saved to the guestbook.xml. Line 72 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:

-==-

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:

-==-

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 9 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 15. The XslTranform class is used to transform the XML elements into HTML. In line 18-19, I am loading the guestbook.xsl with the help of a XslTransform object. Line 22 creates a new object of class XmlNodeList. With the help of this class we can select the required nodes. In line 24 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 25, I have assigned the xml data to the memorystream. The function Seek in line 26, sets the current position to zero. In line 28, I have created an object of the class StreamReader. This class is used to read the stream. Line 31 prints then 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:

XSL:

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:

-==-

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

-==-

Conclusion:

As you see it is not very difficult to create a guestbook. I hope to release further versions of the Guestbook with your help. Please send feedback to sonu@codefinger.de.

Download the examples

Codefinger was founded 2000 by Sonu Kapoor. Sonu has studied E-Commerce in India and currently lives in Germany. After graduation he worked for several companies in Germany. He currently works as a Network Consultant and Software Developer. He has written many applications in various programming languages like VC++, ASP, ASP.NET, XML or XSL. Besides his passion for developing applications, Sonu writes articles for several major websites like Developer.com. He also works as a freelancer for CodeGuru.

Your Own Guestbook In ASP.NET
Comments Off
This entry was posted in Business.
About Sonu Kapoor
Codefinger was founded 2000 by Sonu Kapoor. Sonu has studied E-Commerce in India and currently lives in Germany. After graduation he worked for several companies in Germany. He currently works as a Network Consultant and Software Developer. He has written many applications in various programming languages like VC++, ASP, ASP.NET, XML or XSL. Besides his passion for developing applications, Sonu writes articles for several major websites like Developer.com. He also works as a freelancer for CodeGuru. WebProNews Writer
Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom