VS2005中动态生成树状菜单
2008-10-25 12:45 $等待$ 阅读(433) 评论(0) 收藏 举报一个像CSDN一样的树型菜单导航,用的是VS2005自带的TreeView控件。主要代码是生成树的递归函数。
后台代码
using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11public partial class _Default : System.Web.UI.Page
12{
13 SqlConnection Conn = new SqlConnection("server=OEM-MICRO;database=Test;uid=user;pwd=");
14 DataSet ds;
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Conn.Open();
18 this.createDataSet();
19 Conn.Close();
20 this.InitTree(tvMenu.Nodes, "0");
21 }
22 private DataSet createDataSet()
23 {
24 ds = new DataSet();
25 string sqlStr = "select * from Tree ";
26 SqlDataAdapter cmdSelect = new SqlDataAdapter(sqlStr, Conn);
27 cmdSelect.Fill(ds, "Tree");
28 return ds;
29 }
30 protected void InitTree(TreeNodeCollection Nds, string parentId)//用递归方法动态生成节点
31 {
32 DataView dv = new DataView();
33 TreeNode tmpNode;
34 dv.Table = ds.Tables["Tree"];
35 dv.RowFilter = "ParentId=" + "'" + parentId + "'";
36 foreach (DataRowView drv in dv)
37 {
38 tmpNode = new TreeNode();
39 tmpNode.Value = drv["Id"].ToString();
40 tmpNode.Text = drv["Name"].ToString();
41 tmpNode.NavigateUrl = "#";
42 Nds.Add(tmpNode);
43 this.InitTree(tmpNode.ChildNodes, tmpNode.Value);
44 }
45 }
46}
47

页面代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" >
4
<head runat="server">
5
<title>无标题页</title>
6
</head>
7
<body>
8
<form id="form1" runat="server">
9
<div>
10
11
<asp:TreeView ID="tvMenu" runat="server" ImageSet="Faq" ShowLines="True" ExpandDepth="0" Target="middle" >
12
<ParentNodeStyle Font-Bold="False" />
13
<HoverNodeStyle Font-Underline="True" ForeColor="Purple" />
14
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" />
15
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px"
16
NodeSpacing="0px" VerticalPadding="0px" />
17
</asp:TreeView>
18
19
</div>
20
</form>
21
</body>
22
</html>
23
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">3
<html xmlns="http://www.w3.org/1999/xhtml" >4
<head runat="server">5
<title>无标题页</title>6
</head>7
<body>8
<form id="form1" runat="server">9
<div>10
11
<asp:TreeView ID="tvMenu" runat="server" ImageSet="Faq" ShowLines="True" ExpandDepth="0" Target="middle" >12
<ParentNodeStyle Font-Bold="False" />13
<HoverNodeStyle Font-Underline="True" ForeColor="Purple" />14
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" />15
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px"16
NodeSpacing="0px" VerticalPadding="0px" />17
</asp:TreeView>18
19
</div>20
</form>21
</body>22
</html>23

浙公网安备 33010602011771号