Simple Guestbook Html Code with File System
If the website is designed and developed only in plain HTML, it is nearly impossible to create a Guest Book with just plain HTML in server to track the guests. But now a days it is easier to find an Asp.Net enabled host for cheaper prices. It should be noted that, the server side execution is required for saving a GuestBook. Though the databases are the way to go. To make the article simpler I have used a flat file for GuestBook. As the target of this article is not providing a full working application for GuestBook, I am just writing some code for beginners to learn how to start with.
Guestbook Html Code To write into File System
As we discussed little earlier, the guest book is going to be saved into the file system. Since the flat file is just a text storage, we need to delimit the data in a proper way. so that later on the data can be presented nicely. There are two segments we need to consider for delimiters, one is for the row delimiter another is a column delimiter. so we will use ~ to differentiate each row. and with in the row we can use | to delimit the columns
Source Code
Guestbook Html Code Markup (*.Aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 100%">
<div style="width: 20%; float: left">
Name
</div>
<div style="width: 80%; float: left">
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
</div>
</div>
<div style="width: 100%">
<div style="width: 20%; float: left">
E-mail
</div>
<div style="width: 80%; float: left">
<asp:TextBox runat="server" ID="txtEmail"></asp:TextBox>
</div>
</div>
<div style="width: 100%">
<div style="width: 20%; float: left">
Comments
</div>
<div style="width: 80%; float: left">
<asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox>
</div>
</div>
special characters not allowed
<asp:Button runat="server" ID="btnSubmit" Text="Post" />
</form>
</body>
</html>
Code Behind (*.vb)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim GuestBookContent = txtName.Text & " | " & _
txtEmail.Text & " | " & _
txtComments.Text & " ~ "
My.Computer.FileSystem.WriteAllText(Server.MapPath("~/GuestBook.txt"), GuestBookContent, True)
End Sub
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
End Sub
End Class
Guestbook html code To Parse Text file
The saved GuestBook.Txt can be parsed again using guestbook html code with another one aspx file to load in to the screen by using a Split () function
'~' To delimit a guest entry
'|' To delimit attributes as name email and comments
First split using ‘~’, will get the entire guest book entry for one visitor. Take that entry and parse again using split with |, First element is Name, Second element is e-mail, third element is comments. It can be displayed easily with some HTML formatting.
Source Code
Markup
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="List.aspx.vb" Inherits="ListGuests" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Name" DataField="GuestName"/>
<asp:BoundField HeaderText="Email" DataField="Email" />
<asp:BoundField HeaderText="Content" DataField="Content" HtmlEncode="false" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
Imports System.Data
Partial Class ListGuests
Inherits System.Web.UI.Page
Protected Sub Page_Load(
ByVal sender
As Object,
ByVal e
As System.EventArgs)
Handles Me.Load
If (Not IsPostBack)
Then BindGrid()
End If End Sub Private Sub BindGrid()
Dim GuestBook
As New DataTable
Dim GuestsLogged
As String Dim GuestBookPath
As String = Server.MapPath(
"~/Guestbook.txt")
GuestsLogged = My.Computer.FileSystem.ReadAllText(GuestBookPath)
GuestBook.Columns.Add(
New DataColumn(
"GuestName"))
GuestBook.Columns.Add(
New DataColumn(
"Email"))
GuestBook.Columns.Add(
New DataColumn(
"Content"))
For Each Guest In GuestsLogged.Split("~")
If (Guest.Split("|").Length > 2) Then
Dim GuestRow As DataRow = GuestBook.NewRow
GuestRow("GuestName") = Guest.Split("|")(0)
GuestRow("Email") = Guest.Split("|")(1)
GuestRow("Content") = Guest.Split("|")(2).Replace(vbCrLf, "<br />")
GuestBook.Rows.Add(GuestRow)
End If
Next
GridView1.DataSource = GuestBook
GridView1.DataBind()
End Sub
End Class