ASP.NET TreeView根據資料庫動態產生

來源:互聯網
上載者:User
資料庫結構
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.