zoukankan      html  css  js  c++  java
  • TreeView控件递归绑定数据学习

    将表中的字段封装成属性:文件名(Demo_Area.cs)

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    /// <summary>
    ///Demo_Area 的摘要说明
    /// </summary>
    public class Demo_Area
    {
     public Demo_Area()
     {
      //
      //TODO: 在此处添加构造函数逻辑
      //
     }
        private int id;
        private string name;
        private int pid;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

    }

    页面中的代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;

    public partial class TreeView : System.Web.UI.Page
    {

        //连接数据库
        private static string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            BindTree("0");
        }

        #region 绑定父节点(第一级)
        private void BindTree(string pid)
        {
            DataSet ds = GetData(pid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    TreeNode node = new TreeNode();
                    node.Text = ds.Tables[0].Rows[i]["Name"].ToString();
                    node.Value = ds.Tables[0].Rows[i]["ID"].ToString();
                    this.TreeView1.Nodes.Add(node);
                    BindNode(node);
                }
            }
        }
        #endregion

        #region 绑定子节点
        private void BindNode(TreeNode nd)
        {
            DataSet ds = GetData(nd.Value);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = ds.Tables[0].Rows[i]["Name"].ToString();
                node.Value = ds.Tables[0].Rows[i]["ID"].ToString();

                nd.ChildNodes.Add(node);

                BindNode(node);
            }
        }
        #endregion

        #region 将父级下的子级查询出来,并返回一张虚拟表
        private DataSet GetData(string pid)
        {
            SqlConnection con = new SqlConnection(connstr);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Area_MS where Pid=" + pid, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        #endregion

    }

    效果图:

  • 相关阅读:
    【转】c语言中的#号和##号的作用
    stm32 HAL库编程驱动控制文件<stm32f1xx_hal_conf.h>的使能方法
    rt-thread是如何做到通过menuconfig配置将相应文件加入工程和从工程中除去
    rt-thread 学习路线
    stm32使用rt-thread在文件《stm32f1xx_hal.h》中头文件包含顺序引出的错误
    rt-thread之stm32系列BSP制作方法
    使用rt-thread中BSP-stm32 ENV构建工具报错
    rt-thread中动态内存分配之小内存管理模块方法的一点理解
    内存对齐
    rt-thread中线程内置定时器的作用 ---
  • 原文地址:https://www.cnblogs.com/_ymw/p/yanmingwei.html
Copyright © 2011-2022 走看看