XmlDataSource.OnTransforming(EventArgs) Método
Definição
Gera o evento Transforming antes que o controle XmlDataSource execute uma transformação XSLT em seus dados XML.Raises the Transforming event before the XmlDataSource control performs an XSLT transformation on its XML data.
protected:
virtual void OnTransforming(EventArgs ^ e);
protected virtual void OnTransforming (EventArgs e);
abstract member OnTransforming : EventArgs -> unit
override this.OnTransforming : EventArgs -> unit
Protected Overridable Sub OnTransforming (e As EventArgs)
Parâmetros
Exemplos
O exemplo de código a seguir demonstra como usar um XmlDataSource controle com um TreeView controle para exibir dados XML transformados.The following code example demonstrates how to use an XmlDataSource control with a TreeView control to display transformed XML data. A transformação XML é executada usando a folha de estilos indicada pela TransformFile propriedade.The XML transformation is performed using the style sheet indicated by the TransformFile property. Além disso, a folha de estilos usa argumentos de transformação que são passados para o controle da fonte de dados em um XsltArgumentList objeto em tempo de execução.Additionally, the style sheet uses transform arguments that are passed to the data source control in an XsltArgumentList object at runtime. O exemplo de código demonstra como passar o XsltArgumentList objeto para o controle da fonte de dados manipulando o Transforming evento com um manipulador de eventos personalizado, que é identificado pelo nome do conjunto de métodos para o OnTransforming método.The code example demonstrates how to pass the XsltArgumentList object to the data source control by handling the Transforming event with a custom event handler, which is identified by name of the method set for the OnTransforming method.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void TransformEventHandler(object sender, EventArgs e) {
// Add a dynamic transformation argument.
DateTime d = new DateTime();
d = DateTime.Now.AddDays(20);
// Create an XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("purchdate", "", d.ToShortDateString());
((XmlDataSource) sender).TransformArgumentList = xslArg;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:XmlDataSource
id="XmlDataSource1"
runat="server"
datafile="bookstore.xml"
transformfile="bookswithdiscount.xsl"
ontransforming="TransformEventHandler" />
<!- TreeView uses hierachical data, so the
XmlDataSource uses an XmlHierarchicalDataSourceView
when a TreeView is bound to it. -->
<asp:treeview
id="TreeView1"
runat="server"
datasourceid="XmlDataSource1">
<databindings>
<asp:treenodebinding Depth="1" datamember="genre"
textfield="name" valuefield="name"/>
<asp:treenodebinding depth="2" datamember="book"
textfield="title" valuefield="title"/>
<asp:treenodebinding depth="3" datamember="chapter"
textfield="name" valuefield="num"/>
</databindings>
</asp:treeview>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<script runat="server">
Private Sub TransformEventHandler(sender As Object, e as EventArgs)
' Add a dynamic transformation argument.
Dim d As New DateTime
d = DateTime.Now.AddDays(20)
'Create an XsltArgumentList.
Dim xslArg As XsltArgumentList = New XsltArgumentList
xslArg.AddParam("purchdate", "", d.ToShortDateString())
Dim aXmlDataSource as XmlDataSource = CType(sender, XmlDataSource)
aXmlDataSource.TransformArgumentList = xslArg
End Sub ' TransformEventHandler
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:xmldatasource
id="XmlDataSource1"
runat="server"
datafile="bookstore.xml"
transformfile="bookswithdiscount.xsl"
ontransforming="TransformEventHandler" />
<!- TreeView uses hierachical data, so the
XmlDataSource uses an XmlHierarchicalDataSourceView
when a TreeView is bound to it. -->
<asp:treeview
id="TreeView1"
runat="server"
datasourceid="XmlDataSource1">
<databindings>
<asp:treenodebinding depth="1" datamember="genre"
textfield="name" valuefield="name"/>
<asp:treenodebinding depth="2" datamember="book"
textfield="title" valuefield="title"/>
<asp:treenodebinding depth="3" datamember="chapter"
textfield="name" valuefield="num"/>
</databindings>
</asp:treeview>
</form>
</body>
</html>
O arquivo XML no exemplo de código tem os seguintes dados:The XML file in the code example has the following data:
<bookstore>
<genre name="fiction">
<book ISBN="0000000000">
<title>Secrets of Silicon Valley</title>
<price>12.95</price>
<chapters>
<chapter num="1" name="Introduction" />
<chapter num="2" name="Body" />
<chapter num="3" name="Conclusion" />
</chapters>
</book>
</genre>
<genre name="novel">
<book genre="novel" ISBN="1111111111">
<title>Straight Talk About Computers</title>
<price>24.95</price>
<chapters>
<chapter num="1" name="Introduction" />
<chapter num="2" name="Body" />
<chapter num="3" name="Conclusion" />
</chapters>
</book>
</genre>
</bookstore>
A transformação XML é executada usando a folha de estilos a seguir.The XML transformation is performed using the following style sheet.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="purchdate"/>
<xsl:template match="bookstore">
<bookstore>
<xsl:apply-templates select="genre"/>
</bookstore>
</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"/>
Price: <xsl:value-of select="price"/>
15% discount if purchased by: <xsl:value-of select="$purchdate"/>
</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>
Comentários
A geração de um evento invoca o manipulador de eventos por meio de um delegado.Raising an event invokes the event handler through a delegate. Para obter mais informações sobre como lidar com eventos, consulte manipulando e gerando eventos.For more information about how to handle events, see Handling and Raising Events.
O OnTransforming método também permite que classes derivadas manipulem o evento sem anexar um delegado.The OnTransforming method also allows derived classes to handle the event without attaching a delegate. Essa é a técnica preferida para manipular o evento em uma classe derivada.This is the preferred technique for handling the event in a derived class.
Notas aos Herdeiros
Ao substituir OnTransforming(EventArgs) em uma classe derivada, certifique-se de chamar o OnTransforming(EventArgs) método da classe base para que os delegados registrados recebam o evento.When overriding OnTransforming(EventArgs) in a derived class, be sure to call the OnTransforming(EventArgs) method of the base class so that registered delegates receive the event.