07.17.03
 By
Sonu Kapoor
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:
01: <?xml version="1.0" encoding="ISO-8859-1"?>
02: <guestbook>
03: <guest>
04: <name>Sonu Kapoor</name>
05: <location>Germany</location>
06: <email>sonu@codefinger.de</email>
07: <website>www.codefinger.de</website>
08: <comment>This guestbook is written by Sonu Kapoor.
09: I hope you like it. To learn how to create such a guestbook,
10: read the whole story on my website.</comment>
11: </guest>
12: </guestbook>
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:
01: <% @Page Language="C#" Debug="true" Src="Guestbook.cs" Inherits="Guestbook" %>
02: <form runat="server">
03: ...
04: ...doing some visualisation stuff
05: ...
06: <ASP:Textbox id="name" size="64" runat="server"/>
07:
08: <asp:RequiredFieldValidator id="nameRequired" runat="server"
ControlToValidate="name"
09: ErrorMessage="You must enter a value into textbox1"
Display="dynamic">Enter name 09: </
10: asp:RequiredFieldValidator>
11:
12: <ASP:Textbox id="location" size="64" runat="server"/>
13:
14: <asp:RequiredFieldValidator id="locationRequired" runat="server"
ControlToValidate="location"
15: ErrorMessage="You must enter a value into textbox1"
Display="dynamic">Enter location </
16: asp:RequiredFieldValidator>
17:
18: <ASP:Textbox id="website" size="64" runat="server"/>
19: <ASP:Textbox id="email" size="64" runat="server"/>
20: <ASP:Textbox id="comment" TextMode="Multiline" columns="50" rows="10"
wrap="true"
21: runat="server"/>
22:
23: <asp:RequiredFieldValidator id="commentRequired" runat="server"
ControlToValidate="comment"
24: ErrorMessage="You must enter a value into textbox1"
Display="dynamic">Enter comment </
25: asp:RequiredFieldValidator>
26:
27: <ASP:Button id="submit" runat="server" Text="Submit" OnClick="Save_Comment"/>
28: <ASP:Button id="reset" runat="server" Text="Reset"/>
29: ...
30: ...doing some visualisation stuff
31: ...
32: </script>
</form>
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:
Click Here to Read the Full Article
About the Author:
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.
Read this newsletter at: http://www.webproasp.com/2003/0717.html |
|

 |
| • |
Get
in-touch with industry experts and leaders |
| • |
Make
Freelance contacts |
| • |
Post
your site for review by expert and peers |
| • |
Ask
SEO, Marketing, Development and Design questions |
| • |
Join
a community of your peers in all fields, ready to help |
|
|
|
|
|