數(shù)據(jù)庫表 TreeViews id int類型 主鍵自動(dòng)增長 name 目錄名稱 pid 父目錄id urlPath url路徑
效果圖
aspx頁面文件放一個(gè)treeview控件 <body> <form id="form1" runat="server"> <div> <br /> <asp:TreeView ID="TreeView1" runat="server"> </asp:TreeView> </div> </form> -------------------------------------------------------------------------------------------------------------------------------- aspx.cs文件內(nèi)容: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections; using System.Collections.Generic; using web.DAL; //添加的引用 項(xiàng)目內(nèi)部的 using web.Model;//實(shí)體 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { tree(); } protected void tree() { TreeNode node = new TreeNode(); //初始化根目錄 node.Text = "根目錄"; addnode(0)//0為根節(jié)點(diǎn) TreeView1.Nodes.Add(node); //添加根目錄到treeview控件 } private void addNode(int NodeId) { List<TreeViews> list = TreeViewManager.getTvById(NodeId); //查詢節(jié)點(diǎn)下的節(jié)點(diǎn) foreach (TreeViews tv in list) { TreeNode node = new TreeNode(); node.Text = tv.Name; ////節(jié)點(diǎn)名稱 node.NavigateUrl = tv.UrlPath; ////節(jié)點(diǎn)路徑 node.ChildNodes.Add(node); addNode(tv.Id) //遞歸添加節(jié)點(diǎn) } } } }
--------------------------------------------------------------------------------------------------------------------------------- web.DAL的TreeViewManager.cs類文件 using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using System.Data.Sql; using web.Model; namespace web.DAL { public class TreeViewManager { public static List<TreeViews> getTvById(int id) { string sql = "select * from Treeviews where ORDER BY Id asc "; SqlParameter [] values={ new SqlParameter("@id",id) }; List<TreeViews> list = getDataTable(sql, values); return list; } private static List<TreeViews> getDataTable(string sql,SqlParameter [] values) { using (DataTable dt = DBHelper.GetDataTable(sql, values)) { List<TreeViews> list = new List<TreeViews>(); foreach (DataRow row in dt.Rows) { TreeViews tv = new TreeViews(); tv.Id =(int) row["id"]; tv.Name = row["name"].ToString(); tv.PId=(int)row["pid"]; tv.UrlPath = row["UrlPath"].ToString(); list.Add(tv); } return list; } } } } ------------------------------------------------------------------------------------------------------------------------------- DBHelper.cs類訪問數(shù)據(jù)庫的 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; namespace web.DAL { public class DBHelper { private static readonly string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; //獲取web.config的網(wǎng)站的數(shù)據(jù)庫.賬號(hào).密碼 private static SqlConnection CreateConnection()//創(chuàng)建連接數(shù)據(jù)庫 { SqlConnection conn = new SqlConnection(constr); if (conn.State==ConnectionState.Closed) { conn.Open(); return conn; } else { return conn; } }
//執(zhí)行增刪改的.不過在本項(xiàng)目中沒有用到 public static int ExcuteCommand(string sql, SqlParameter[] values) { int i = 0; using (SqlConnection conn = CreateConnection()) { SqlCommand comd = new SqlCommand(sql, conn); if (values != null) { comd.Parameters.AddRange(values); } i = comd.ExecuteNonQuery(); return i; } } //執(zhí)行讀取的.不過在本項(xiàng)目中沒有用到
public static SqlDataReader GetReader(string sql,SqlParameter [] values) { SqlConnection conn = CreateConnection(); SqlCommand comd = new SqlCommand(sql,conn); if (values != null) { comd.Parameters.AddRange(values); } SqlDataReader rd = comd.ExecuteReader(CommandBehavior.CloseConnection); return rd; } //返回DataSet的.不過在本項(xiàng)目中沒有用到
public static DataSet GetDateSet(string sql,SqlParameter [] values) { using(SqlConnection conn=CreateConnection()) { SqlDataAdapter ap = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); if (values != null) ap.SelectCommand.Parameters.AddRange(values); ap.Fill(ds); return ds; } } //返回DataTable表 主要用這個(gè)了 public static DataTable GetDataTable(string sql,SqlParameter [] values) { using (SqlConnection conn = CreateConnection()) { SqlDataAdapter ap = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); if (values != null) { ap.SelectCommand.Parameters.AddRange(values); } ap.Fill(dt); return dt; } } } } ------------------------------------------------------------------------------------------------------------------------ Treeviews.cs實(shí)體類 using System; using System.Collections.Generic; using System.Text; namespace web.Model { public class TreeViews { int id; string name; int pId; string urlPath; public TreeViews() { } public string UrlPath { get { return urlPath; } set { urlPath = value; } } public int PId { get { return pId; } set { pId = value; } } public string Name { get { return name; } set { name = value; } } public int Id { get { return id; } set { id = value; } } } } 注:因?yàn)橹虚g層沒什么東西.就省略了.直接用的DAL層的. http://hi.baidu.com/royler
|