Click here to Skip to main content
15,885,278 members
Articles / Web Development / IIS
Article

ASP.NET Guest Entry Form

Rate me:
Please Sign up or sign in to vote.
1.88/5 (4 votes)
8 Aug 20043 min read 104.9K   629   25   3
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.

Image 1

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
<?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.
    VB
    Dim xdoc As New XmlDocument
    xdoc.Load(Server.MapPath("guestbook.xml"))
  2. The checkbox entry will be retrieved and set to a text value.
    VB
    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:
    VB
    Dim elem As XmlElement
    elem = xdoc.CreateElement("guest")
    elem.SetAttribute("private", prev)
  4. A new 'Guest' will be prepended to this file via:
    VB
    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:
    VB
    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:
    VB
    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:
    VB
    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:
    vnet
    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:
    VB
    Dim strDate As String
    strDate = DateTime.Now.ToLongDateString() + " - " + _
              DateTime.Now.ToLongTimeString()
    addTextElement(xdoc, elem, "date", strDate)
  11. Finally, the document will be saved via:
    VB
    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
<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:

Image 2

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


Written By
Technical Writer Hodentek
United States United States
Worked in the area of electrical discharges, high energy lasers, high voltage technology, plasma technology, lithography, thin film plastics, superconducting thin films, diamond thin films, electron accelerators, and free electron lasers for several years. Mentored/guided MS and PhD students at several universities in USA, Brazil, Australia, and India.
Reading books and photography are my hobbies.

Also trained workforce clients with legacy computer skills in web related technologies.

I recently authored a beginner level book on MS SQL Server Integration Services. Details available at the following link:

http://www.packtpub.com/sql-server-integration-services-visual-studio-2005/book

My second book was released in 2008
Learn SQL Server Reporting Services 2008

Get book details at the following site:
http://www.packtpub.com/learning-sql-server-2008-reporting-services/book

This is for anyone who is interested in Reporting Services a la Microsoft. It has over 50 hands-on exercises and covers all aspects of Reporting Services.

Recent new books:

Microsoft SQL Azure Enterprise Application Development 2010
-A Comprehensive book on SQL Azure

Microsoft Visual Studio LightSwitch Business Application Development 2011
A step-by-step approach that is sure to work

Learning SQL Server Reporting Services 2012 Packt Publishers, ISBN: 978-1-84968-992-2 , 2013

Comments and Discussions

 
Generalu Pin
babu175-May-06 3:47
babu175-May-06 3:47 
Generalgreat article Pin
Guinevere Smith22-Sep-05 20:11
sussGuinevere Smith22-Sep-05 20:11 
GeneralAn article Pin
Danelkits8-Jan-15 0:06
Danelkits8-Jan-15 0:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.