2012-01-03 11 views
3

ASP.Net 2.0を使用しており、実行時にGridViewとXMLDataSourceを使用して変換されたXMLデータを表示しようとしています。ここで実行時のASP.Net GridViewとXMLDataSourceバインディング

は(input.xmlに)私のXMLデータである:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:template match="/"> 
    <CDCatalog> 
     <xsl:apply-templates/> 
    </CDCatalog> 
</xsl:template> 

<xsl:template match="cd"> 
    <CD> 
    <xsl:apply-templates select="*"> 
     </xsl:apply-templates> 
    </CD> 
</xsl:template> 


<xsl:template match="catalog/cd/*"> 
      <xsl:attribute name="{local-name()}"> 
     <xsl:value-of select="."/> 
    </xsl:attribute> 
</xsl:template> 

</xsl:stylesheet> 

そして、あなたの参考のために、これは変換されたXMLのようになります。

<catalog> 
<cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
</cd> 
<cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
</cd> 
<cd> 
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
</cd> 
<cd> 
    <title>Still got the blues</title> 
    <artist>Gary Moore</artist> 
    <country>UK</country> 
    <company>Virgin records</company> 
    <price>10.20</price> 
    <year>1990</year> 
</cd> 
</catalog> 

そして、ここでは(Transform.xslt)変換であります:

<CDCatalog> 
<CD title="Empire Burlesque" artist="Bob Dylan" country="USA" company="Columbia" price="10.90" year="1985"/> 
<CD title="Hide your heart" artist="Bonnie Tyler" country="UK" company="CBS Records" price="9.90" year="1988"/> 
<CD title="Greatest Hits" artist="Dolly Parton" country="USA" company="RCA" price="9.90" year="1982"/> 
<CD title="Still got the blues" artist="Gary Moore" country="UK" company="Virgin records" price="10.20" year="1990"/> 
</CDCatalog> 

今、この私は私のC#コード(GridView1が.aspxのページ上で作成された)でやって何を:

 XmlDataSource xmlDS = new XmlDataSource(); 
     xmlDS.EnableCaching = false; 
     xmlDS.DataFile = "~/Input.xml"; 
     xmlDS.TransformFile = "~/Transform.xslt"; 
     xmlDS.XPath = "/CDCatalog/CD"; 
     GridView1.DataSourceID = xmlDS.ID; 
     GridView1.DataBind(); 
     GridView1.Visible = true; 

GridViewにデータが表示されません。私はインターネット上でかなり調べてみましたが、設計時にaspxページでこの種のことを話しますが、コードでは実行時ではありません。私はこれがGridView/XMLDataSourceバインディングの制限であるかどうか、あるいは何か間違っているかどうかはわかりません。私はこれに関する助けに感謝します。 おかげで、スリニバス

答えて

2

単純にこの行を変更:これに

GridView1.DataSourceID = xmlDS.ID; 

GridView1.DataSource = xmlDS; 

enter image description here

+0

ます。またGridView1.Visible = trueを削除することができますが、他の場所の可視性を変更しない限り、デフォルトで表示されます。 –

+0

DataSourceIDの代わりにDataSourceを使用します。 XMLDataSourceの新しいインスタンスを作成するとき、IDプロパティは空白に設定されているため、xmlDS.IDの設定が機能していない可能性があります。私はソースを今見つけることができませんでしたが、私はあなたがグリッドのソート能力を失う可能性があるDataSource自体を設定する場合、いくつかの場所を読むと思う。本当にそうだと思いますか?私はこれが私が使用することになっているかどうかはわかりませんが、回答は私の最初の質問に答えているので、回答を回答として設定しています。私が使用しているオプションは、空のデータソースをasp.netデザインページに置き、IDで使用することです。 –

関連する問題