Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass a XSLT parameter to a XmlDataSource correctly?
    primarykey
    data
    text
    <p>I believe I have all the code correct but I can't get it to work. The GridView has allowSorting = true. So in theory, when I click on the column header the xml in the gridview should sort by that column. The XML shows in the GridView, but doesn't sort at all. I'm stumped.</p> <p>DST_Test.Xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;root&gt; &lt;data name="Test1.Text" xml:space="preserve"&gt; &lt;value&gt;Please Pick Bare Pump&lt;/value&gt; &lt;comment&gt;Tab - Pump Configuration&lt;/comment&gt; &lt;/data&gt; &lt;data name="Test2.Text" xml:space="preserve"&gt; &lt;value&gt;Complete&lt;/value&gt; &lt;comment&gt;A07&lt;/comment&gt; &lt;/data&gt; &lt;data name="Test3.Text" xml:space="preserve"&gt; &lt;value&gt;Confirmed&lt;/value&gt; &lt;comment&gt;A01&lt;/comment&gt; &lt;/data&gt; &lt;/root&gt; </code></pre> <p>DataSrcTransform.xslt</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;xsl:param name="sortby"&gt;&lt;/xsl:param&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;!--&lt;xsl:template match="@* | node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@* | node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt;--&gt; &lt;xsl:template match="root"&gt; &lt;root&gt; &lt;xsl:apply-templates select="data"&gt; &lt;xsl:sort select="$sortby"/&gt; &lt;/xsl:apply-templates&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="data"&gt; &lt;data&gt; &lt;xsl:attribute name="ctrlname"&gt; &lt;xsl:value-of select="@name"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:attribute name="value"&gt; &lt;xsl:value-of select="value" /&gt; &lt;/xsl:attribute&gt; &lt;xsl:attribute name="comment"&gt; &lt;xsl:value-of select="comment" /&gt; &lt;/xsl:attribute&gt; &lt;/data&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Code-behind</p> <pre><code>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 </code></pre> <p>HTML</p> <pre><code>&lt;div&gt; &lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" PageSize="25" AutoGenerateColumns="False"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="ctrlname" HeaderText="ctrlname" SortExpression="ctrlname" /&gt; &lt;asp:BoundField DataField="value" HeaderText="value" SortExpression="value" /&gt; &lt;asp:BoundField DataField="comment" HeaderText="comment" SortExpression="comment" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;asp:XmlDataSource ID="XmlDataSource1" runat="server"&gt; &lt;/asp:XmlDataSource&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload