zoukankan      html  css  js  c++  java
  • TreeView控件数据绑定之:XML文件数据绑定

    效果展示:

    XML文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <root id="0" name="左侧导航栏">
      <foods id="1" name="食物">
        <foods id="11" name="生菜"></foods>
        <foods id="12" name="白菜">
          <foods id="121" name="大白菜"></foods>
          <foods id="122" name="小白菜"></foods>
          <foods id="123" name="圆白菜"></foods>
        </foods>
      </foods>
      <fruids id="2" name="水果">
        <fruids id="21" name="桃子"></fruids>
        <fruids id="22" name="葡萄">
          <fruids id="221" name="紫葡萄"></fruids>
        </fruids>
      </fruids>
      <clotheses id="3" name="服饰">
        <clotheses id="31" name="鞋子"></clotheses>
        <clotheses id="32" name="毛衣"></clotheses>
      </clotheses>
    </root>

    前段代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index2.aspx.cs" Inherits="GuaidMenu.Index2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>TreeView控件数据绑定之:XML文件数据绑定</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" ImageSet="Arrows" LineImagesFolder="~/TreeLineImages">
                <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                <NodeStyle Font-Names="Verdana" Font-Size="8pt" 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="Xmldata.xml"></asp:XmlDataSource>
        </div>
        </form>
    </body>
    </html>

    注意:TreeView控件的DataSourceID属性的值是XmlDataSource控件ID的值

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace GuaidMenu
    {
        public partial class Index2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                TreeView1.ShowLines = false;  //将线显示出来
                TreeNodeBinding tnb = new TreeNodeBinding();
                tnb.DataMember = "root";
                tnb.ValueField = "name";
                this.TreeView1.DataBindings.Add(tnb);
    
                TreeNodeBinding tnd = new TreeNodeBinding();
                tnd.DataMember = "foods";
                tnd.ValueField = "name";
                TreeView1.DataBindings.Add(tnd);   //将食物绑定到控件上
    
                TreeNodeBinding ftnb = new TreeNodeBinding();
                ftnb.DataMember = "fruids";
                ftnb.ValueField = "name";
                this.TreeView1.DataBindings.Add(ftnb);  //将水果绑定到控件上
    
                TreeNodeBinding ctnb = new TreeNodeBinding();
                ctnb.DataMember = "clotheses";
                ctnb.ValueField = "name";
                this.TreeView1.DataBindings.Add(ctnb);   //将服饰绑定到控件上
            }
        }
    }

    写写博客,方便自己也方便有需要的人*_*!

  • 相关阅读:
    left join 多表关联查询
    Dictionary解析json,里面的数组放进list,并绑定到DataGridView指定列
    C#同一位置切换显示两个Panel内容
    C#点击按钮用DataGridView动态增加行、删除行,增加按钮列
    C#获取本机mac地址
    C# MD5加密
    C# SQLiteDataReader获得数据库指定字段的值
    linux下mongodb安装、服务器、客户端、备份、账户命令
    ubuntu下创建python的虚拟环境
    python多进程之间的通信:消息队列Queue
  • 原文地址:https://www.cnblogs.com/Yisijun/p/4752316.html
Copyright © 2011-2022 走看看