嵌套的Repeater使用XMLNodeList

时间:2009-11-14 15:02:27

标签: asp.net nested-repeater xmlnodelist

我遇到了问题。我想我可能接近解决方案,但似乎无法到达那里。我有一个XMLDoc,它从存储的Proc加载到数据集中,并从中提取xml,(不要认为我可以更改任何这部分)。 XML看起来像这样:

<data>
  <datanode>
    <name>11</name>
    <group>G1</group>
    <value>val1</value>
  </datanode>
  <datanode>
    <name>12</name>
    <group>G1</group>
    <value>val2</value>
  </datanode>  
  <datanode>
<name>21</name>
<group>G2</group>
<value>val1</value>
  </datanode>
  <datanode>
<name>22</name>
<group>G2</group>
<value>val2</value>
  </datanode>
  <datanode>
<name>23</name>
<group>G2</group>
<value>val3</value>
  </datanode>  
  <datanode>
<name>31</name>
<group>G3</group>
<value>val1</value>
  </datanode>
  <datanode>
<name>32</name>
<group>G3</group>
<value>val2</value>
  </datanode>
  <datanode>
<name>33</name>
<group>G3</group>
<value>val3</value>
  </datanode>
  <datanode>
<name>34</name>
<group>G3</group>
<value>val4</value>
  </datanode>
</data>

我需要使用嵌套转发器来显示此信息。我想要的是通过“组”节点对其进行分组,然后显示属于该“组”节点的每个“数据节点”。所以它在页面上看起来像这样:

G1 - has 2 nodes
11 - Val1
12 - Val2

G2 - has 3 nodes
21 - Val1
22 - Val2
23 - Val3

etc

到目前为止我所拥有的: Default.aspx的:

    <asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate>
        <p>                
            COUNT: <asp:Literal runat="server" ID="Literal3" Text='<%# XPath("count(group)") %>' />                
            <br />Text: <asp:Literal runat="server" ID="Literal4" Text='<%# XPath("text()") %>' />
            <br />Group: <asp:Literal runat="server" ID="Literal2" Text='<%# XPath("group") %>' />
            <br />ID: <asp:Literal runat="server" ID="displayname" Text='<%# XPath("name") %>' />
            <br />VAL: <asp:Literal runat="server" ID="Literal1" Text='<%# XPath("value") %>' />
         </p>            

    </ItemTemplate>

    </asp:Repeater>

Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    private XmlNodeList nodelist;

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"C:\development\testforWork\WebApplication1\WebApplication1\data.xml");

        nodelist = doc.SelectNodes("//datanode[not(group=preceding-sibling::datanode/group)]/group");           
        //nodelist = doc.SelectNodes("//datanode[group='G2']");

        Repeater1.DataSource = nodelist;
        Repeater1.DataBind();
    }
}

我可以让他们一个接一个地显示每个datanode,但我无法弄清楚如何嵌套转发器并进行分组。我对XPath和转发器很新,并且认为我必须在我的知识上有一些差距而且无法连接点(除非我完全跑错了路)。

由于

1 个答案:

答案 0 :(得分:4)

好的,经过一些实验后找到了我的解决方案:

<asp:Repeater ID="repManifestNumber" runat="server" DataSource="<%# ManifestDataTemp %>">
                    <ItemTemplate>
                            Manifest Number:
                            <%# CurrentManifestNumber = XPath("manifest_number").ToString() %>                                
                             - <%# XPath("count(..//box_item[manifest_number=" + CurrentManifestNumber + "])")%> Box(s)

                        <div class="showhide">
                                <asp:Repeater ID="repMainfestDetail" runat="server" DataSource='<%# ((XmlNode)Container.DataItem).SelectNodes("..//box_item[manifest_number=" + CurrentManifestNumber + "]") %>'>
                                    <HeaderTemplate>
                                        <table class="orders" id="manifest_<%# CurrentManifestNumber %>">
                                            <tr>
                                                <th>
                                                    Box Number
                                                </th>
                                                <th>
                                                    Date Dispatched
                                                </th>
                                                <th>
                                                    <input type="checkbox" class="checkboxflipflop_local" />
                                                    - Select All
                                                </th>
                                            </tr>
                                            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr class="odd-table-row">
                                            <td>
                                                <asp:Literal runat="server" ID="litBoxNumber" Text='<%# XPath("box_number") %>' />
                                            </td>
                                            <td>
                                                <asp:Literal runat="server" ID="litErpDispatched" Text='<%# Convert.ToDateTime(XPath("date_dispatched")).ToString("dd/MM/yyyy") %>' />
                                            </td>
                                            <td>
                                                <asp:Literal runat="server" ID="litHiddenXmlNode" Text='<%# ((XmlNode)Container.DataItem).OuterXml %>'
                                                    Visible="false"></asp:Literal>
                                                <asp:CheckBox ID="chkIsReceived" runat="server" Checked="false" CssClass="storecheckbox" />
                                            </td>
                                        </tr>
                                    </ItemTemplate>

                                </asp:Repeater>                                
                        </table>
                        </div>
                    </ItemTemplate>

                </asp:Repeater>

希望这可能在将来帮助其他人(或者当我再次忘记它时帮助我)。