資料庫結構
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