如何在asp.net的项目符号列表中显示数据?

时间:2014-10-15 05:20:18

标签: asp.net

我是asp.net的新手,我希望在Bulleted list中显示数据。我使用以下Foreach循环获取项目,并希望在asp.net中的Bulleted list中显示

Result.aspx.cs

foreach (XmlElement statement1 in node2statements.ChildNodes)
{
var st = statement1.InnerText.ToString();                      
}

Result.aspx

<ul id="section1">
<li>
</li>
</ul>

2 个答案:

答案 0 :(得分:1)

我做了一个小例子来演示如何将数据从xml文件绑定到bulletedList。我使用了XmlDataSource个对象。首先我使用了一个xml文件

<强> products.xml

<?xml version="1.0" encoding="utf-8" ?>
<list>
  <product id="123">
    <name>product a</name>
    <price>123.45</price>
  </product>
  <product id="123">
    <name>product b</name>
    <price>123.45</price>
  </product>
</list>

我创建了转换文件 - 因为只有属性可以在控件中访问。积分转到Darin Dimitrov以指出

  

... XmlDataSource中的一个错误,它阻止您绑定到xml的值   节点。它仅适用于属性

<强> transform.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="list">
    <list>
      <xsl:apply-templates select="product"/>
    </list>
  </xsl:template>
  <xsl:template match="product">
    <product>
      <xsl:attribute name="id">
        <xsl:value-of select="@id"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:value-of select="name"/>
      </xsl:attribute>
      <xsl:attribute name="price">
        <xsl:value-of select="price"/>
      </xsl:attribute>
    </product>
  </xsl:template>
</xsl:stylesheet>

然后我做了两个例子 - 第一个展示如何在页面中使用

<form id="form1" runat="server">
    <div>
        <h3>Bulleted List with XML binding</h3>
        <asp:BulletedList ID="blPageOnly" runat="server" DataValueField="id" DataTextField="name" DataSourceID="xmlSource"></asp:BulletedList>
        <asp:XmlDataSource ID="xmlSource" runat="server" DataFile="~/app_data/products.xml" TransformFile="~/app_data/transform.xsl" XPath="list/product"></asp:XmlDataSource>
    </div>
    <div>
        <h3>Bulleted List with XML binding - code behind</h3>
        <asp:BulletedList ID="blCodeBehind" runat="server"></asp:BulletedList>
    </div>
</form>

第二个展示如何从后面的代码中绑定xml文件

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        XmlDataSource xmlSource = new XmlDataSource()
        {
            DataFile = "~/app_data/products.xml",
            XPath="list/product",
            TransformFile = "~/app_data/transform.xsl"
        };

        blCodeBehind.DataSource = xmlSource;
        blCodeBehind.DataTextField = "name";
        blCodeBehind.DataValueField = "id";

        blCodeBehind.DataBind();
    }
}

将导致

BulletedList databinding with XmlDataSource

我希望你能调整我的代码来帮助你!

答案 1 :(得分:0)

使用asp:Repeater并将其与您的子节点绑定

在转发器的项目模板中,您可以定义UL和LI并将它们分配给statement1

相关问题