【问题标题】:ASP.Net GridView and XMLDataSource binding at run timeASP.Net GridView 和 XMLDataSource 在运行时绑定
【发布时间】:2014-10-13 15:18:25
【问题描述】:

我正在使用 ASP.Net 2.0 并尝试在运行时使用 GridView 和 XMLDataSource 显示转换后的 xml 数据。

这是我的 xml 数据(Input.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):

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

<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 绑定的限制,或者我做错了什么。我很感激这方面的任何帮助。 谢谢,斯里尼瓦斯

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    只需更改此行:

    GridView1.DataSourceID = xmlDS.ID;
    

    到这里:

    GridView1.DataSource = xmlDS;
    

    【讨论】:

    • 也可以去掉 GridView1.Visible = true;默认情况下它是可见的,除非您在其他地方更改可见性。
    • 使用 DataSource 代替 DataSourceID 有效。当我们创建 XMLDataSource 的新实例时,ID 属性设置为空白,这可能就是设置 xmlDS.ID 不起作用的原因。我现在找不到源,但我想我读过一些地方,如果你设置 DataSource 本身,你可能会失去网格的排序能力。你认为真的是这样吗?我将您的回复设置为答案,因为它回答了我最初的问题,但我不确定这是否是我最终会使用的。我正在使用的选项是在 asp.net 设计页面上放置一个空白数据源并按 ID 使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多