共用方式為


XmlDataSource.OnTransforming(EventArgs) 方法

定義

Transforming 控制項對其 XML 資料執行 XSLT 轉換之前,引發 XmlDataSource 事件。

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)

參數

e
EventArgs

包含事件資料的 EventArgs

範例

下列程式碼範例示範如何使用 XmlDataSource 控制項搭配 TreeView 控制項來顯示已轉換的 XML 資料。 XML 轉換是使用 屬性所 TransformFile 指示的樣式表單來執行。 此外,樣式表單會使用在執行時間傳遞至 物件中資料來源控制項的 XsltArgumentList 轉換引數。 程式碼範例示範如何使用 XsltArgumentList 自訂事件處理常式處理 Transforming 事件,以方法集合 OnTransforming 的方法名稱來識別物件至資料來源控制項。

<%@ 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>

程式碼範例中的 XML 檔案具有下列資料:

<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>  

XML 轉換是使用下列樣式表單來執行。

<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>  

備註

引發事件會透過委派叫用此事件處理常式。 如需如何處理事件的詳細資訊,請參閱 處理和引發事件

OnTransforming 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。

給繼承者的注意事項

在衍生類別中覆 OnTransforming(EventArgs) 寫時,請務必呼叫 OnTransforming(EventArgs) 基類的 方法,讓已註冊的委派接收事件。

適用於

另請參閱