2011-05-18 71 views
0

我相信我所有的代碼都是正確的,但我無法使它正常工作。 GridView的allowSorting = true。所以理論上,當我點擊列標題時,gridview中的xml應該按該列排序。 XML顯示在GridView中,但根本不排序。我很難過。如何正確地將XSLT參數傳遞給XmlDataSource?

DST_Test.Xml

<?xml version="1.0" encoding="utf-8" ?> 
<root> 
    <data name="Test1.Text" xml:space="preserve"> 
     <value>Please Pick Bare Pump</value> 
     <comment>Tab - Pump Configuration</comment> 
    </data> 
    <data name="Test2.Text" xml:space="preserve"> 
     <value>Complete</value> 
     <comment>A07</comment> 
    </data> 
    <data name="Test3.Text" xml:space="preserve"> 
     <value>Confirmed</value> 
     <comment>A01</comment> 
    </data> 
</root> 

DataSrcTransform.xslt

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

    <xsl:param name="sortby"></xsl:param> 

    <xsl:output method="xml" indent="yes"/> 

    <!--<xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template>--> 
    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="data"> 
       <xsl:sort select="$sortby"/> 
      </xsl:apply-templates> 
     </root> 
    </xsl:template> 
    <xsl:template match="data"> 
     <data> 
      <xsl:attribute name="ctrlname"> 
       <xsl:value-of select="@name"/> 
      </xsl:attribute> 
      <xsl:attribute name="value"> 
       <xsl:value-of select="value" /> 
      </xsl:attribute> 
      <xsl:attribute name="comment"> 
       <xsl:value-of select="comment" /> 
      </xsl:attribute> 
     </data> 
    </xsl:template> 
</xsl:stylesheet> 

代碼隱藏

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender 

     If Not IsPostBack Then 
      XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml" 
      XmlDataSource1.XPath = "//data" 
      XmlDataSource1.TransformFile = xsltFileName 
      GridView1.DataSource = XmlDataSource1 
      GridView1.DataBind() 
     End If 
End Sub 

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting 

     Select Case e.SortExpression 

      Case "ctrlname" 
       sortAttr = "@name" 
      Case "value" 
       sortAttr = "value" 
      Case "comment" 
       sortAttr = "comment" 
     End Select 

     Dim xslTrnsform As System.Xml.Xsl.XsltArgumentList = New System.Xml.Xsl.XsltArgumentList 
     xslTrnsform.AddParam("sortby", "", sortAttr) 
     XmlDataSource1.EnableCaching = False 
     XmlDataSource1.TransformArgumentList = xslTrnsform 
     XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml" 
     XmlDataSource1.XPath = "//data" 
     XmlDataSource1.TransformFile = xsltFileName 
     GridView1.DataSource = XmlDataSource1 
     GridView1.DataBind() 
End Sub 

HTML

<div> 
      <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
       PageSize="25" 
       AutoGenerateColumns="False"> 
       <Columns> 
        <asp:BoundField DataField="ctrlname" HeaderText="ctrlname" 
         SortExpression="ctrlname" /> 
        <asp:BoundField DataField="value" HeaderText="value" SortExpression="value" /> 
        <asp:BoundField DataField="comment" HeaderText="comment" 
         SortExpression="comment" /> 
       </Columns> 
      </asp:GridView> 
</div> 
<asp:XmlDataSource ID="XmlDataSource1" runat="server">    
</asp:XmlDataSource> 

回答

1

我不知道其他方面,但我相信你的XSL中有一些問題不能正確排序數據。嘗試使用此XSL(通過評論默認排序):現在

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:param name="sortby" select="'comment'"/> 

    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="data"> 
       <xsl:sort select="*[name()=$sortby]" order="ascending"/> 
      </xsl:apply-templates> 
     </root> 
    </xsl:template> 

    <xsl:template match="data"> 
     <data> 
      <xsl:attribute name="ctrlname"> 
       <xsl:value-of select="@name"/> 
      </xsl:attribute> 
      <xsl:attribute name="value"> 
       <xsl:value-of select="value" /> 
      </xsl:attribute> 
      <xsl:attribute name="comment"> 
       <xsl:value-of select="comment" /> 
      </xsl:attribute> 
     </data> 
    </xsl:template> 
</xsl:stylesheet> 

編輯,包括排序順序作爲參數(默認升序):

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:param name="sortby" select="'comment'"/> 
    <xsl:param name="order" select="'ascending'"/> 

    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="data"> 
       <xsl:sort select="*[name()=$sortby]" order="{$order}"/> 
      </xsl:apply-templates> 
     </root> 
    </xsl:template> 

    <xsl:template match="data"> 
     <data> 
      <xsl:attribute name="ctrlname"> 
       <xsl:value-of select="@name"/> 
      </xsl:attribute> 
      <xsl:attribute name="value"> 
       <xsl:value-of select="value" /> 
      </xsl:attribute> 
      <xsl:attribute name="comment"> 
       <xsl:value-of select="comment" /> 
      </xsl:attribute> 
     </data> 
    </xsl:template> 
</xsl:stylesheet> 

說明:

  • *[name()=$sortby]選擇名稱等於我們的參數的所有節點後代$sortby
  • order="{$order}"用於使用參數設置訂單屬性的值。值可以是ascendingdescending

請注意,默認情況下執行排序時假定爲text數據類型。

+0

謝謝!你的答案是100%的現貨!我需要訂單參數,我不得不改變語法。我確實有一個問題:* [name()= sortby]和{$ order}語法有什麼作用? – dotnetN00b 2011-05-18 18:13:27

+0

@ dotnetN00b:我很高興它有幫助。我已經添加了一些解釋。再見 – 2011-05-18 18:54:10