日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Guestbook for ASP.NET - The Code Project - AS...

 thy 2007-10-16

Contents

Introduction

It is a project permitting people to sign a guestbook on a website. The project is built in two parts:

  • Signing the guestbook.
  • Viewing the guestbook.

Database

The guestbook will be stored in an XML file on the server, named guestbook.xml. The encoding of the XML file is changed to ISO-8859-1 to be able to handle special characters. Here is the structure of the XML file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<guestbook>
<guest private="yes">
<name>Laurent Kemp?t;/name>
<homepage url="http://perso./laurent.kempe/">Tech Head</homepage>
<location>Illzach, France</location>
<comment>First to sign the guestbook ;)</comment>
<date>Thursday, May 30, 2002 - 10:29 AM</date>
</guest>
</guestbook>

You will be prompted to enter the following information:

  • Name
  • E-Mail
  • Homepage title
  • Homepage URL
  • Location
  • Comments
  • PRIVATE - I only want the web site owner to see my email

Application

To be free to change the way the guestbook is displayed, it is needed to separate the coding from the data. To achieve this requirement, I chose to use XSLT to transform the XML file to an HTML file returned to the client.

Signing

The page that will permit people to sign the guestbook is the Web Form called 'Sign.aspx'. This page will require that the user fill some textboxes with information to be displayed on the guestbook. To be able to validate the information, we use RequiredFieldValidator. We also use a RegularExpressionValidator to validate the email address.

When the guest has filled all his information, he presses the Continue button and the page gets back the event in the method ButtonContinue_Click. The method loads the XML database, retrieves the information entered by the guest and adds them at the beginning of the XML file. Then the new database is saved on the server disk and the guest is redirected to the view page.

Collapse
private void ButtonContinue_Click(object sender, System.EventArgs e)
{
//Load guestbook database
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load( Server.MapPath("guestbook.xml") );
//Get private status
string strPrivate;
if ( CheckBoxPrivate.Checked )
strPrivate = "yes";
else
strPrivate = "no";
//Create a new element
XmlElement elem = xmldoc.CreateElement("guest");
elem.SetAttribute("private", strPrivate);
//Add the new guest as the first node
xmldoc.DocumentElement.PrependChild(elem);
addTextElement( xmldoc, elem, "name", TextBoxName.Text );
addTextElement( xmldoc, elem, "email", TextBoxEMail.Text );
addTextElement( xmldoc, elem, "homepage", TextBoxHomepageTitle.Text );
XmlAttribute newAttr = xmldoc.CreateAttribute("url");
newAttr.Value = TextBoxHomepageURL.Text;
elem.LastChild.Attributes.Append( newAttr );
addTextElement( xmldoc, elem, "location", TextBoxLocation.Text );
addTextElement( xmldoc, elem, "comment", TextBoxComments.Text );
//Write date
string strDate = DateTime.Now.ToLongDateString() +
" - " +
DateTime.Now.ToLongTimeString();
addTextElement( xmldoc, elem, "date", strDate );
xmldoc.Save( Server.MapPath("guestbook.xml") );
Response.Redirect("view.aspx");
}

We use the method addTextElement to build the new guest entry into the database:

private void addTextElement( XmlDocument doc, XmlElement nodeParent,
string strTag, string strValue )
{
XmlElement nodeElem = doc.CreateElement( strTag );
XmlText nodeText = doc.CreateTextNode( strValue );
nodeParent.AppendChild( nodeElem );
nodeElem.AppendChild( nodeText );
}

Viewing

To view all guestbook entries, we add another Web Form called 'View.aspx' to the project. In the Page_Load method of the page, we load the XML database and the XSLT file. We do the transformation and output the result in a Literal Web Form control.

Collapse
private void Page_Load(object sender, System.EventArgs e)
{
//Load guestbook database from the xml file
XmlDocument doc = new XmlDocument( );
doc.Load( Server.MapPath("guestbook.xml") );
// Get the page number asked
string strPageAsked = Request.QueryString["page"];
// If the page is not defined then set it to first one
if ( strPageAsked == null )
{
strPageAsked = "1";
}
int nGuestPerPage = 5;
int nGuests = doc.ChildNodes[1].ChildNodes.Count;
int nPageAsked = System.Convert.ToInt32(strPageAsked);
int lowerbound = 1 + ( nPageAsked - 1 ) * nGuestPerPage;
int upperbound = lowerbound + nGuestPerPage - 1;
//Do XSLT transformation
XslTransform xslt = new XslTransform();
xslt.Load( Server.MapPath("guestbook.xslt") );
//Build XLST Param list
XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParam("lowerbound", "", lowerbound.ToString());
xsltArgs.AddParam("upperbound", "", upperbound.ToString());
//Transform XML to HTML
MemoryStream ms = new MemoryStream();
xslt.Transform( doc, xsltArgs, ms );
ms.Seek( 0, SeekOrigin.Begin );
StreamReader sr = new StreamReader(ms);
//Insert result in the View.aspx page
LiteralGuests.Text = sr.ReadToEnd();
//Insert the pages navigator at the bottom of the page
int nPages = 0;
if (( nGuests % nGuestPerPage) != 0 )
nPages = 1 + (nGuests / nGuestPerPage);
else
nPages = (nGuests / nGuestPerPage);
LiteralGuests.Text += "Page(s) ";
for (int n = 1; n <= nPages; n++)
{
LiteralGuests.Text += "<font face='verdana' size='2'>"
LiteralGuests.Text += "<a href='/Guestbook/View.aspx?page=";
LiteralGuests.Text += n.ToString();
LiteralGuests.Text += "'>";
LiteralGuests.Text += n.ToString();
LiteralGuests.Text += "</a></font> ";
}
sr.Close();
}

All transformation from XML to HTML is done in the guestbook.xslt file. This transformation requests two parameters: lowerbound and upperbound, representing the lower and upper indexed values according to the guestbook page to display. Basically, what we do is to loop from lower bound to upper bound and do the transformation:

<xsl:for-each
select="http://guest[position() <= $upperbound and position() >= $lowerbound]">
<xsl:apply-templates select="name"/>
</xsl:for-each>

This is, for example, the transformation used to display a guest with its email, if it is not defined as private:

<xsl:template match="name">
<xsl:choose>
<xsl:when test="../@private='yes'">
<font face="verdana" size="2">
<b><xsl:value-of select='.' /></b>
</font>
</xsl:when>
<xsl:otherwise>
<font face="verdana" size="2">
<b><a HREF="mailto:{../email}"><xsl:value-of select='.' ></a></b>
</font>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

You may look at the file guestbook.xslt for further details.

Conclusion

I would say that you gain to separate data from processes, and in this matter, XML helps a lot. If you would like to change the looks of the guestbook view, you need only to change the guestbook.xslt file.

Problems Faced

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多