zoukankan      html  css  js  c++  java
  • ASP.NET TreeView根据数据库动态生成

    数据库结构
    Tb_department表
    D_ID 部门编号
    D_Name 部门名称
    D_Tel 联系电话
    D_Address 联系地址
    D_Chief 负责人,
    D_Belong 所属部门

    Tb_employee表
    E_ID 职工编号
    E_Name 职工姓名
    E_Sex 职工性别
    E_Tel 联系电话
    E_Address 联系地址
    E_Birth 出生年月
    D_Name 所属部门

    前台aspx
    <asp:TreeView ID="TreeView1" runat="server" ImageSet="Faq" Width="162px">
                        <ParentNodeStyle Font-Bold="False" />
                        <HoverNodeStyle Font-Underline="True" ForeColor="Purple" />
                        <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" />
                        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px"
                            NodeSpacing="0px" VerticalPadding="0px" />
    </asp:TreeView>

    后台代码aspx.cs
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TreeViewBind();
            }
        }
        #region 主从表绑定
        private void TreeViewBind()
        {
            SqlConnection cn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["Mispersonalconn"].ConnectionString);
            SqlCommand mycmd1 = new SqlCommand("select * from [Tb_department]", cn1);
            SqlDataAdapter da1 = new SqlDataAdapter(mycmd1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1, "Tb_department");//读出大类

            for (int i = 0; i < ds1.Tables["Tb_department"].Rows.Count; i++)
            {
                TreeNode td1 = new TreeNode();
                TreeNode departmentlist = new TreeNode("部门列表","","","","");
                TreeView1.Nodes.Add(departmentlist);
                td1.Text = ds1.Tables["Tb_department"].Rows[i]["D_Name"].ToString();//大类部门列表
                td1.Value = ds1.Tables["Tb_department"].Rows[i]["D_ID"].ToString();
                td1.NavigateUrl = "~/WebFiles/List.aspx?D_ID=" + td1.Value + "&" + "D_Name=" + td1.Text;//连接地址
                departmentlist.ChildNodes.Add(td1);

                SqlConnection cn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Mispersonalconn"].ConnectionString);
                SqlCommand mycmd2 = new SqlCommand("select * from [Tb_employee] where D_Name = '" + ds1.Tables["Tb_department"].Rows[i]["D_Name"].ToString() + "'", cn2);
                SqlDataAdapter da2 = new SqlDataAdapter(mycmd2);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Tb_employee");//读出小类

                for (int j = 0; j < ds2.Tables["Tb_employee"].Rows.Count; j++)
                {
                    TreeNode td2 = new TreeNode();
                    td2.Text = ds2.Tables["Tb_employee"].Rows[j]["E_Name"].ToString();//小类
                    //td2.NavigateUrl = "~/WebFiles/List.aspx?D_ID={0}&D_Name={1}";//连接地址
                    td1.ChildNodes.Add(td2);
                }
            }

            TreeView1.DataBind();
        }
        #endregion

  • 相关阅读:
    【深度学习Deep Learning】资料大全
    在谷歌中缓存下载视频离线观看,js代码
    asp.net mvc 中Html.ValidationSummary显示html
    asp.net网站访问时不能显示页面
    k8s install kubeadm网络原因访问不了谷哥and gpg: no valid OpenPGP data found. 解决办法
    火绒杀毒软件更安静
    Linux使用mount挂载Windows共享文件夹
    spark学习
    https://blog.csdn.net/tangdong3415/article/details/53432166
    正则表达式
  • 原文地址:https://www.cnblogs.com/hakuci/p/1131810.html
Copyright © 2011-2022 走看看