Article
Create Your Own Guestbook In ASP.NET
Recently I was working on my Website, and decided I wanted to implement a guestbook. I started to search the Web to find the best guestbook for my Website, but when none turned up, I thought 'Hey I'm a developer, why not create my own?'
It was very easy to create a guestbook -- you can do it too. In this tutorial, I'll show you how. I'll assume that you have already knowledge about the basics of ASP.NET programming, that you know the techniques involved in codebehind, and that you have some XML/XSL skills.
Overview
What do we need in order to create a guestbook? We need two Web forms: one in which the user can enter their name, email address, and comment, and another that's used to display these comments as they're signed into the guestbook. Of course we can build this functionality into one Web form, but to have a clean code, I'll use two Web forms with several codebehind files (I'll discuss these in more detail in a moment).
We'll also need a database to hold the information entered via the form. I used a simple XML file (a database) to store the information entered by the user. For the visualisation of the XML we'll use XSL.
So, in summary, we need the following:
- Two Web forms
- Codebehind
- Database
- XSL
In a guestbook, it's usually sufficient to store a user's name, location, email address, Website address, and comment. Of course, you can store more fields, but for our purposes, these are enough. We'll store this data in the XML file, which will look something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<guestbook>
<guest>
<name>Sonu Kapoor</name>
<location>Germany</location>
<email>sonu@codefinger.de</email>
<website>www.codefinger.de</website>
<comment>This guestbook is written by Sonu Kapoor.
I hope you like it. To learn how to create such a guestbook,
read the whole story on my website.</comment>
</guest>
</guestbook>
Signing the Guestbook
We'll allow the user to 'sign' our guestbook by entering some information into a simple Web form -- in our example this is the guestbook.aspx file. I use the following fields in the Web form:
- Name
- Location
- Website
- Comment
Here's the code:
<% @Page Language="C#" Debug="true" Src="Guestbook.cs"
Inherits="Guestbook" %>
<form runat="server">
...
...doing some visualisation stuff
...
<ASP:Textbox id="name" size="64" runat="server"/>
<asp:RequiredFieldValidator id="nameRequired" runat="server"
ControlToValidate="name"
ErrorMessage="You must enter a value into textbox1"
Display="dynamic">Enter name
</asp:RequiredFieldValidator>
<ASP:Textbox id="location" size="64" runat="server"/>
<asp:RequiredFieldValidator id="locationRequired" runat="server"
ControlToValidate="location" ErrorMessage="You must enter
a value into textbox1" Display="dynamic">
Enter location </asp:RequiredFieldValidator>
<ASP:Textbox id="website" size="64" runat="server"/>
<ASP:Textbox id="email" size="64" runat="server"/>
<ASP:Textbox id="comment" TextMode="Multiline"
columns="50" rows="10" wrap="true" runat="server"/>
<asp:RequiredFieldValidator id="commentRequired" runat="server"
ControlToValidate="comment" ErrorMessage="You must enter
a value into textbox1" Display="dynamic">
Enter comment </asp:RequiredFieldValidator>
<ASP:Button id="submit" runat="server" Text="Submit"
OnClick="Save_Comment"/>
<ASP:Button id="reset" runat="server" Text="Reset"/>
...
...doing some visualisation stuff
...
</script>
</form>
To avoid confusing you with unnecessary code, I have removed the visualisation tags -- including table, table header etc. -- from this example (though, of course, these are all included in the downloadable code that's provided at the end of this tutorial). As we only display a simple form with a few fields and buttons, you can't see any real programming code in this file. This is because all the functionality is hidden in the codebehind.
In the first line of the code above, I set the SRC attribute to let the ASP.NET file know that we are using the codebehind file Guestbook.cs I've also set the attribute Inherits with the corresponding classname. This attribute lets the file know which class to inherit.
Next, I've implemented the required text fields. Remember that if you want to use the same variables in the codebehind, they need to have the same ID in both files, and they must be declared as public.
In the next section of the code, I used the ASP.NET validator controls. These controls check whether the user has entered a value into the text field, without doing a round-trip to the server. The code is executed on the client side.
Finally, I implemented a submit button with an OnClick event called Save_Comment. This event is used to store the information entered into the XML file by the user. The function of this event is available in Guestbook.cs. I also implemented a reset button -- and that's it! Nothing more has to be done to the Web form. Now, if you run the guestbook.aspx, you should see a Web form that looks like this:

Sonu is founder of the company codefinger. He has developed software in several languages such as C, C++, VC++, Java, ASP, XML, XSL, JScript, and VBScript.