![]() |
Web Development »
Custom Controls »
General
Intermediate
ASP.NET Guest Entry FormBy mysorianGuest Entry form with an XML control. |
VB, XML.NET 1.0, .NET 1.1, WinXP, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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]:
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>
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>
When the submit button is clicked, the following code will be executed in order:
XMLDocument 'Guestbook.xml' will be loaded. Dim xdoc As New XmlDocument
xdoc.Load(Server.MapPath("guestbook.xml"))
Dim prev As String
If (CheckBoxPrivate.Checked) Then
prev = "yes"
Else
prev = "no"
End If
private" is an attribute of the "guest" [root] element: Dim elem As XmlElement
elem = xdoc.CreateElement("guest")
elem.SetAttribute("private", prev)
xdoc.DocumentElement.PrependChild(elem)
addTextElement function will be used, that takes,
XmlDocument as a constant,
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
name", "email", "homepage" via: addTextElement(xdoc, elem, "name", TextBoxName.Text)
addTextElement(xdoc, elem, "email", TextBoxEMail.Text)
addTextElement(xdoc, elem, "homepage", TextBoxHomepageTitle.Text)
Dim newatt As XmlAttribute
newatt = xdoc.CreateAttribute("url")
newatt.Value = TextBoxHomepageURL.Text
elem.LastChild.Attributes.Append(newatt)
location", "comment" via: addTextElement(xdoc, elem, "location", TextBoxLocation.Text)
addTextElement(xdoc, elem, "comment", TextBoxComments.Text)
Dim strDate As String
strDate = DateTime.Now.ToLongDateString() + " - " + _
DateTime.Now.ToLongTimeString()
addTextElement(xdoc, elem, "date", strDate)
xdoc.Save(Server.MapPath("guestbook.xml"))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:

| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Aug 2004 Editor: Smitha Vijayan |
Copyright 2004 by mysorian Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |