Mega Code Archive

 
Categories / ASP.Net / XML
 

Handling XmlDataSource Events

<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml.Xsl" %> <script runat="server"> protected void bookSource_Transforming(object sender, EventArgs e)  {   int discountPercentage = 10;     XsltArgumentList argList = new XsltArgumentList();   argList.AddParam("discount", "", discountPercentage.ToString());   ((XmlDataSource) sender).TransformArgumentList = argList; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>Handling XmlDataSource Events</title> </head> <body>         <form id="form1" runat="server">     <div>         <asp:GridView ID="bookView" Runat="server" DataSourceID="bookSource" AutoGenerateColumns="False">             <Columns>                 <asp:BoundField HeaderText="ISBN" DataField="ISBN" SortExpression="ISBN"></asp:BoundField>                 <asp:BoundField HeaderText="Title" DataField="Title" SortExpression="Title"></asp:BoundField>                 <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price"></asp:BoundField>                 <asp:BoundField HeaderText="Discount" DataField="Discount" SortExpression="Discount"></asp:BoundField>             </Columns>         </asp:GridView>                 <asp:XmlDataSource ID="bookSource" Runat="server" DataFile="~/Data.xml"             XPath="Data/genre[@name ='Fiction']/book" TransformFile="~/Data_with_parameter.xsl"             OnTransforming="bookSource_Transforming">                     </asp:XmlDataSource>&nbsp;     </div>     </form> </body> </html> File: ~/Data.xml <Data>   <genre name="Fiction">     <book ISBN="1" Title="title 1" Price="19.99" Discount="1.999">       <chapter num="1" name="Introduction">         Abstract...       </chapter>       <chapter num="2" name="Body">         Abstract...       </chapter>       <chapter num="3" name="Conclusion">         Abstract...       </chapter>     </book>   </genre>   <genre name="NonFiction">     <book ISBN="2" Title="title 2" Price="27.95" Discount="2.795">       <chapter num="1" name="Introduction">         Abstract...       </chapter>       <chapter num="2" name="Body">         Abstract...       </chapter>       <chapter num="3" name="Conclusion">         Abstract...       </chapter>     </book>   </genre> </Data> File: ~/Data_with_parameter.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:param name="discount"/>   <xsl:template match="Data">     <Data>       <xsl:apply-templates select="genre"/>     </Data>   </xsl:template>   <xsl:template match="genre">     <genre>       <xsl:attribute name="name">         <xsl:value-of select="@name"/>       </xsl:attribute>       <xsl:apply-templates select="book"/>     </genre>   </xsl:template>   <xsl:template match="book">     <book>       <xsl:attribute name="ISBN">         <xsl:value-of select="@ISBN"/>       </xsl:attribute>       <xsl:attribute name="title">         <xsl:value-of select="@Title"/>       </xsl:attribute>       <xsl:attribute name="price">         <xsl:value-of select="@Price"/>       </xsl:attribute>       <xsl:attribute name="discount">         <xsl:value-of select="$discount * @Price"/>       </xsl:attribute>       <xsl:apply-templates select="chapters/chapter" />     </book>   </xsl:template>   <xsl:template match="chapter">     <chapter>       <xsl:attribute name="num">         <xsl:value-of select="@num"/>       </xsl:attribute>       <xsl:attribute name="name">         <xsl:value-of select="@name"/>       </xsl:attribute>       <xsl:apply-templates/>     </chapter>   </xsl:template> </xsl:stylesheet>