Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Guest Entry Form

0.00/5 (No votes)
8 Aug 2004 1  
Guest Entry form with an XML control.

Contents

Creating a Guest Book

I was not planning to reinvent the wheel but was trying to reuse the code presented earlier by one of the contributors, Guestbook for ASP.NET by Laurent Kempe. Since the code was in VC++ environment, I changed the code around to suit the VB.NET language. It turned out that between the time the original code was written and now, some of the overloaded functions in the viewPage of that article have become obsolete and other modifications were recommended, especially those pertaining to the XSLT Transform method. In this article, the XML control from the tool box is directly used to produce the guest book in HTML.

Fields required in the Guest Book

Generally, in Guest Books, it is required to collect information from a user browsing your site. You want a guest book to be included to obtain this information.

The following entries are needed in the Guest Book [you may customize]:

  • Name
  • Email
  • Homepage (URL)
  • Location
  • Comment
  • Date
  • a Boolean yes or no for private guests

Guest Entry Page UI

Since the information is to be retrieved from a guest entry page, add a web form and add the various controls. To get all the controls in good alignment, place them all inside a table contained in the form object of this web form.

The following HTML code shows the 'table' nested inside the 'form'.

<form id="Form1" method="post" runat="server">
<TABLE id="Table1" 
    style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" 
    cellSpacing="1" cellPadding="1" width="300" border="1">
    <TR>
        <TD>Name</TD>
        <TD>
            <asp:TextBox id="TextBoxName" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>E-Mail</TD>
        <TD>
            <asp:TextBox id="TextBoxEMail" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>Homepage Title</TD>
        <TD>
            <asp:TextBox id="TextBoxHomepageTitle" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>Homepage URL</TD>
        <TD>
            <asp:TextBox id="TextBoxHomepageURL" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>Location</TD>
        <TD>
            <asp:TextBox id="TextBoxLocation" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>Comments</TD>
        <TD>
            <asp:TextBox id="TextBoxComments" runat="server">
            </asp:TextBox>
        </TD>
    </TR>
    <TR>
        <TD>Private</TD>
        <TD>
            <asp:CheckBox id="CheckBoxPrivate" runat="server">
            </asp:CheckBox>
        </TD>
    </TR>
</TABLE>
<asp:Button id="ButtonContinue" 
     style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 232px"
     runat="server" Text="SignUp" Width="144px" Height="32px">
</asp:Button>
</form>

Persistence of Guest Information

The user entered information will be placed in an XML file rather than in a database. This file shall be called the guestbook.xml and will have the structure shown in guestbook.html.

This is the guestbook.xml page that is already populated with some guest information.

<?xml version="1.0" encoding="ISO-8859-1"?>
<guestbook>
  <guest private="yes">
    <name>David Frost</name>
    <email>df@yahoo.com</email>
    <homepage url="www.frost.com">David Frost News</homepage>
    <location>England</location>
    <comment>Nothingmuch</comment>
    <date>Friday, July 30, 2004 - 5:02:33 PM</date>
  </guest>
  <guest private="no">
    <name>Sandil</name>
    <email>sandil@yahoo.com</email>
    <homepage url="www.Sandilhome.com">UAE</homepage>
    <location>uae</location>
    <comment>test this one</comment>
    <date>Monday, July 26, 2004 - 5:11:12 PM</date>
  </guest>
  <guest private="yes">
    <name>Jayaram Krishnasawamy</name>
    <homepage url="http://www.mysorian.com/htek">Programmer</homepage>
    <location>137 Tennyson Drive</location>
    <comment>So,so</comment>
    <date>Thursday, July 23, 2004 -02:52 PM</date>
  </guest>
</guestbook>

Code behind GuestEntry page with comments

When the submit button is clicked, the following code will be executed in order:

  1. The XMLDocument 'Guestbook.xml' will be loaded.
    Dim xdoc As New XmlDocument
    xdoc.Load(Server.MapPath("guestbook.xml"))
  2. The checkbox entry will be retrieved and set to a text value.
    Dim prev As String
    If (CheckBoxPrivate.Checked) Then
        prev = "yes"
    Else
        prev = "no"
    End If
  3. A new document element is declared and defined as in next code, the checkbox called "private" is an attribute of the "guest" [root] element:
    Dim elem As XmlElement
    elem = xdoc.CreateElement("guest")
    elem.SetAttribute("private", prev)
  4. A new 'Guest' will be prepended to this file via:
    xdoc.DocumentElement.PrependChild(elem)
  5. To add the other entries, a addTextElement function will be used, that takes,
    1. the XmlDocument as a constant,
    2. the element as a constant,
    3. the name of the element, and
    4. the location on the form from where it is retrieved.
  6. The code for the addTextElement function is as follows:
    Private Sub addTextElement(ByVal doc1 As XmlDocument, ByVal _
       elem1 As XmlElement, ByRef strTag As String, ByRef strVal _
       As String)
        Dim nodeElem = doc1.CreateElement(strTag)
        Dim nodeText = doc1.CreateTextNode(strVal)
        elem1.AppendChild(nodeElem)
        nodeElem.AppendChild(nodeText)
    End Sub
  7. Add the other fields, "name", "email", "homepage" via:
    addTextElement(xdoc, elem, "name", TextBoxName.Text)
    addTextElement(xdoc, elem, "email", TextBoxEMail.Text)
    addTextElement(xdoc, elem, "homepage", TextBoxHomepageTitle.Text)
  8. The homepage element will store the URL as its attribute, and therefore we add the following code so that it will be homepage URL's attribute and will be taken from the guest entry form from a text box:
    Dim newatt As XmlAttribute
    newatt = xdoc.CreateAttribute("url")
    newatt.Value = TextBoxHomepageURL.Text
    elem.LastChild.Attributes.Append(newatt)
  9. We add two other fields, namely, "location", "comment" via:
    addTextElement(xdoc, elem, "location", TextBoxLocation.Text)
    addTextElement(xdoc, elem, "comment", TextBoxComments.Text)
  10. Next, the date field will be added combining data and time via:
    Dim strDate As String
    strDate = DateTime.Now.ToLongDateString() + " - " + _
              DateTime.Now.ToLongTimeString()
    addTextElement(xdoc, elem, "date", strDate)
  11. Finally, the document will be saved via:
    xdoc.Save(Server.MapPath("guestbook.xml"))

Viewing the guestbook.xml Page

We first need to create a XSL style sheet to transform the guestbook.xml to a HTML file. The following simple, no-frills file gb.xsl does the conversion.

<html xsl:version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      lang="en">
    <head>
        <title>GuestBook entries</title>
    </head>
    <body>
        <table border="1">
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Homepage</th>
                <th>Location</th>
           <th>Comment</th>
           <th>Date</th>
            </tr>
            <xsl:for-each select="guestbook/guest">
            <tr>
                   <td>
                        <em><xsl:value-of select="name"/></em>
                    </td>
                    <td>
                        <xsl:value-of select="email"/>
                    </td>
                    <td>
                        <xsl:value-of select="homepage"/>
                    </td>
                    <td>
                        <xsl:value-of select="location"/>
                    </td>
            <td>
               <xsl:value-of select="comment"/>
            </td>
            <td>
               <xsl:value-of select="date"/>
            </td>
           </tr>
       </xsl:for-each>
        </table>
    </body>
</html>

We add a view.aspx page and to that we drag a XML control from the tool box. Right click on the XML control and set the two properties as follows:

  • DocumentSource-> guestbook.xml
  • TransformSource-> gb.xsl

Note: You need to give write permissions to your guestbook.xml page, otherwise it will throw an error. Error trapping and validation should be added as required.

Finally, when you bring up view.aspx page, you will see the following:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here