您的位置:首页 > 编程语言 > ASP

ASP.NET TreeView根据数据库动态生成

2008-04-06 07:54 429 查看
数据库结构

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: