First, there is no reason to reinvent the wheel, if you are alredy using an
xslt transform and it does everything you need it to do, leave it alone. On
your edit page you could use an xmldocument object to write/save the data in
the xml document. When the user displays the "View" page the xlst will
reload the xml file and display the edited/updated information. Make sure
the authentication method you have chosen has write access to the xml file,
I used "everyone - write".
Here is a working sample
There are six files in this example, two webforms (View.aspx, Edit.aspx),
two code behinds, an xml document and an xlst file. Set View.aspx as the
startup page.
Hope this helps.
'View.aspx
<HTML>
<HEAD>
<title>Default</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="btnEdit" style="Z-INDEX: 101; LEFT: 12px; POSITION:
absolute; TOP: 13px" runat="server"
Text="Edit" Width="102px"></asp:button></form>
</body>
</HTML>
' End View.aspx
'View.aspx.vb
Public Class View
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents btnEdit As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim doc As New System.Web.UI.WebControls.Xml
Me.Controls.Add(doc)
doc.DocumentSource = "Books.xml"
doc.TransformSource = "Books.xslt"
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click
Response.Redirect("edit.aspx")
End Sub
End Class
' End View.aspx.vb
'Edit.aspx
<HTML>
<HEAD>
<title>Edit</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div>
<asp
ropDownList id="lstBooks" runat="server" Width="189px"
AutoPostBack="True"></asp
ropDownList>
<asp:Button id="btnSave" runat="server" Width="131px"
Text="Save"></asp:Button>
<asp:Button id="btnDone" runat="server" Width="131px"
Text="Finished"></asp:Button>
</div>
<div>
<table runat="server">
<tr width="100%">
<td width="30%">Author</td>
<td runat="server">
<asp:TextBox id="txtAuthor" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Title</td>
<td>
<asp:TextBox id="txtTitle" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Genre</td>
<td>
<asp:TextBox id="txtGenre" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Price</td>
<td>
<asp:TextBox id="txtPrice" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Date Published</td>
<td>
<asp:TextBox id="txtPubDate" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Description</td>
<td>
<asp:TextBox id="txtDescription" runat="server" Width="100%"
TextMode="MultiLine"></asp:TextBox></td>
</tr>
</table>
</div>
</form>
</body>
</HTML>
'End Edit.aspx
'Edit.aspx.vb
Public Class Edit
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents btnSave As System.Web.UI.WebControls.Button
Protected WithEvents lstBooks As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtAuthor As System.Web.UI.WebControls.TextBox
Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents txtGenre As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPubDate As System.Web.UI.WebControls.TextBox
Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
Protected WithEvents btnDone As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim xDoc As System.Xml.XmlDocument
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack _
AndAlso (Session.Item("IsDirty") = True _
OrElse Session.Item("IsDirty") Is Nothing) Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
Dim Nodes As System.xml.XmlNodeList = xDoc.SelectNodes("//book")
For Each Node As System.Xml.XmlNode In Nodes
Me.lstBooks.Items.Add(Node.Item("title").InnerText)
Next
LoadData()
Session.Item("IsDirty") = False
End If
End Sub
Private Sub LoadData()
Me.txtAuthor.Text = GetElement("author")
Me.txtDescription.Text = GetElement("description")
Me.txtGenre.Text = GetElement("genre")
Me.txtPrice.Text = GetElement("price")
Me.txtPubDate.Text = GetElement("publish_date")
Me.txtTitle.Text = GetElement("title")
End Sub
Private Function GetElement(ByVal TagName As String) As String
If xDoc Is Nothing Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
End If
Dim Path As String = "//book[title=""" & Me.lstBooks.SelectedValue &
"""]"
Dim Node As System.Xml.XmlNode = xDoc.SelectSingleNode(Path)
Return Node.Item(TagName).InnerText
End Function
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
If xDoc Is Nothing Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
End If
Dim Path As String = "//book[title=""" & Me.lstBooks.SelectedValue &
"""]"
Dim node As System.Xml.XmlNode = xDoc.SelectSingleNode(Path)
With node
.Item("title").InnerText = Me.txtTitle.Text
.Item("author").InnerText = Me.txtAuthor.Text
.Item("description").InnerText = Me.txtDescription.Text
.Item("publish_date").InnerText = Me.txtPubDate.Text
.Item("price").InnerText = Me.txtPrice.Text
.Item("genre").InnerText = Me.txtGenre.Text
End With
xDoc.Save(Server.MapPath("books.xml"))
Session.Item("IsDirty") = True
End Sub
Private Sub lstBooks_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles lstBooks.SelectedIndexChanged
LoadData()
End Sub
Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDone.Click
Response.Redirect("view.aspx")
End Sub
End Class
'End Edit.aspx.vb
'Books.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
<xsl:template match="catalog">
<HTML>
<BODY>
<TABLE BORDER="2">
<TR>
<TD>Title</TD>
<TD>Author</TD>
<TD>Genre</TD>
<TD>Price</TD>
<TD>Date Published</TD>
<TD>Description</TD>
</TR>
<xsl:for-each select="book">
<TR>
<TD><xsl:value-of select="title"/></TD>
<TD><xsl:value-of select="author"/></TD>
<TD><xsl:value-of select="genre"/></TD>
<TD><xsl:value-of select="price" /></TD>
<TD><xsl:value-of select="publish_date" /></TD>
<TD><xsl:value-of select="description" /></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
'End Books.xslt
'Books.xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
'End Books.xml