分享

ASP.NET TreeView显示分类

 快乐至上365 2014-02-20

数据库表
TreeViews

id              int类型 主键自动增长
name     目录名称
pid           父目录id
urlPath    url路径

效果图

aspx页面文件放一个treeview控件

<body>
     <form id="form1" runat="server">
     <div>
         <br />
         <asp:TreeView ID="TreeView1" runat="server">
            </asp:TreeView> 
     </div>
    </form>
--------------------------------------------------------------------------------------------------------------------------------
aspx.cs文件内容:

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; //添加的引用 项目内部的
using web.Model;//实体

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为根节点

          TreeView1.Nodes.Add(node); //添加根目录到treeview控件
            
         }
      private void addNode(int NodeId)

      {         

              List<TreeViews> list = TreeViewManager.getTvById(NodeId);      //查询节点下的节点

                foreach (TreeViews tv in list)
                {
                    TreeNode node = new TreeNode();    
                    node.Text = tv.Name;                          ////节点名称
                    node.NavigateUrl = tv.UrlPath;             ////节点路径
                    node.ChildNodes.Add(node);

                    addNode(tv.Id) //递归添加节点

                } 

     }

       
     }
     
}

---------------------------------------------------------------------------------------------------------------------------------

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类访问数据库的
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的网站的数据库.账号.密码

         private static SqlConnection CreateConnection()//创建连接数据库
         {
             SqlConnection conn = new SqlConnection(constr);
             if (conn.State==ConnectionState.Closed)
             {
                 conn.Open();
                 return conn;
             }
             else
             {
                 return conn;
             }
         }

//执行增删改的.不过在本项目中没有用到
         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;
             }           
         }
//执行读取的.不过在本项目中没有用到

         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的.不过在本项目中没有用到

         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表 主要用这个了
         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实体类
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; }
         }

     }
}

注:因为中间层没什么东西.就省略了.直接用的DAL层的.
http://hi.baidu.com/royler

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多