数据绑定TreeView中的记录计数

时间:2013-08-30 14:40:01

标签: c# asp.net xml treeview

我通过XmlDataSource获得了简单的TreeView绑定。这些数据表示内容某些项目的类别。我需要显示这些类别中的记录数。我怎样才能做到这一点?我得到了这段代码:

<asp:TreeView ID="TreeView1" 
        runat="server" 
        DataSourceID="XmlDataSource1" 
        ImageSet="Arrows"
        ExpandDepth="1" 
        OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
        <DataBindings>
            <asp:TreeNodeBinding DataMember="root" TextField="name" ValueField="id"  />
            <asp:TreeNodeBinding DataMember="category" TextField="name" ValueField="id" />
            <asp:TreeNodeBinding DataMember="subcategory" TextField="name" ValueField="id" />
            <asp:TreeNodeBinding DataMember="subsubcategory" TextField="name" ValueField="id" />
        </DataBindings>
        <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
        <NodeStyle Font-Names="Tahoma" 
            Font-Size="10pt" 
            ForeColor="Black" 
            HorizontalPadding="5px"
            NodeSpacing="0px" 
            VerticalPadding="0px" />
        <ParentNodeStyle Font-Bold="False" />
        <SelectedNodeStyle Font-Underline="True" 
            ForeColor="#5555DD" 
            HorizontalPadding="0px"
            VerticalPadding="0px" />
    </asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Categories.xml">
</asp:XmlDataSource>

我应该从这个页面调用一些Sql命令,还是从XmlDataSource调用?谢谢你的建议

1 个答案:

答案 0 :(得分:0)

以下是如何使用TreeNode DataBound事件处理程序和XPathNavigator

继续显示附加到名称的子项数
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
    XmlElement myElement = (XmlElement)e.Node.DataItem;
    // e.Node.NavigateUrl = ...
    // e.Node.Expanded = ...
    e.Node.Text = String.Format("{0} ({1})"
                   ,myElement.Attributes["name"].Value
                   ,myElement.CreateNavigator().Evaluate("count(./child::*)"));
}

希望这会有所帮助