TreeView用法
来源:互联网 发布:电气自动化编程 编辑:程序博客网 时间:2024/06/06 18:12
数据表:
用法一:
if exists (select * from sysobjects where name='Category')drop table Categorygocreate table Category(CategoryId int primary key identity(1,1),CategoryName varchar(32) unique not null,ParentId int)go--添加内置数据--第一级类别insert into Category values('文学',0)insert into Category values('教育',0)insert into Category values('社科',0)insert into Category values('古籍',0)insert into Category values('儿童',0)insert into Category values('外语',0)insert into Category values('专业',0)--第二级类别insert into Category values('小说',1)insert into Category values('诗歌',1)insert into Category values('散文',1)insert into Category values('初等教育',2)insert into Category values('高等教育',2)insert into Category values('哲学',3)insert into Category values('伦理',3)insert into Category values('诸子百家',4)insert into Category values('漫画',5)insert into Category values('童话寓言',5)insert into Category values('教材',6)insert into Category values('外语读物',6)insert into Category values('学术研究',6)insert into Category values('计算机',7)insert into Category values('数理',7)insert into Category values('医药',7)insert into Category values('农业',7)insert into Category values('建材',7)insert into Category values('生物',7)insert into Category values('化工',7)insert into Category values('法律',7)insert into Category values('经融',7)insert into Category values('体育',7)--第三级类别insert into Category values('推理',8)insert into Category values('悬疑',8)insert into Category values('经典',8)insert into Category values('校园',8)insert into Category values('言情',8)insert into Category values('科幻',8)insert into Category values('政治',8)insert into Category values('程序设计',21)insert into Category values('图形图像',21)insert into Category values('网络通信',21)insert into Category values('硬件组维',21)--第四级类别insert into Category values('C\C++',38)insert into Category values('Java',38)insert into Category values('.net',38)insert into Category values('脚本语言',38)insert into Category values('汇编语言',38)insert into Category values('Basic',38)insert into Category values('Delphi',38)select * from Category
后台代码:
DBHelper类:
namespace WebApplication1{ public class DBHelper { protected SqlConnection conn = null; protected SqlCommand comm = null; protected SqlDataAdapter DA = null; protected SqlDataReader DR = null; public DBHelper() { conn = new SqlConnection(ConfigurationSettings.AppSettings["SqlConString"].ToString()); } /// <summary> /// 返回一个DataTable数据集 /// </summary> /// <param name="sql">sql查询语句</param> protected DataTable GetDataTable(string sql) { DA = new SqlDataAdapter(sql, conn); DataTable DT = new DataTable(); DA.Fill(DT); return DT; } /// <summary> /// 返回指定条件的DataTable数据集 /// </summary> /// <param name="sql">Sql查询语句</param> /// <param name="parameter">查询条件</param> protected DataTable GetDataTable(string sql, SqlParameter parameter) { comm = new SqlCommand(sql, conn); comm.Parameters.Add(parameter); DA = new SqlDataAdapter(); DA.SelectCommand = comm; DataTable DT = new DataTable(); DA.Fill(DT); return DT; } /// <summary> /// 返回指定条件的DataTable数据集 /// </summary> /// <param name="sql">Sql查询语句</param> /// <param name="parameters">查询条件</param> protected DataTable GetDataTable(string sql, SqlParameter[] parameters) { comm = new SqlCommand(sql, conn); foreach (SqlParameter parameter in parameters) { comm.Parameters.Add(parameter); } DA = new SqlDataAdapter(); DA.SelectCommand = comm; DataTable DT = new DataTable(); DA.Fill(DT); return DT; } /// <summary> /// 返回单个数据 /// </summary> /// <param name="sql">sql查询语句</param> protected object GetData(string sql) { comm = new SqlCommand(sql, conn); try { conn.Open(); return comm.ExecuteScalar(); } catch (SqlException) { return null; } catch (InvalidOperationException) { return null; } catch { return null; } finally { conn.Close(); } } /// <summary> /// 返回指定条件的单个数据 /// </summary> /// <param name="sql">sql查询语句</param> /// <param name="parameter">查询条件</param> protected object GetData(string sql, SqlParameter parameter) { comm = new SqlCommand(sql, conn); comm.Parameters.Add(parameter); try { conn.Open(); return comm.ExecuteScalar(); } catch (SqlException) { return null; } catch (InvalidOperationException) { return null; } catch { return null; } finally { conn.Close(); } } /// <summary> /// 返回指定条件的单个数据 /// </summary> /// <param name="sql">sql查询语句</param> /// <param name="parameters">查询条件</param> protected object GetData(string sql, SqlParameter[] parameters) { comm = new SqlCommand(sql, conn); foreach (SqlParameter parameter in parameters) { comm.Parameters.Add(parameter); } try { conn.Open(); return comm.ExecuteScalar(); } catch (SqlException) { return null; } catch (InvalidOperationException) { return null; } catch { return null; } finally { conn.Close(); } } /// <summary> /// 执行一个无参存储过程 /// </summary> /// <param name="procName">存储过程名</param> /// <returns>返回一个布尔类型的数据表示是否成功</returns> protected bool ExecuteProc(string procName) { comm = new SqlCommand(procName, conn); //指定操作类型(执行存储过程) comm.CommandType = CommandType.StoredProcedure; try { conn.Open(); return comm.ExecuteNonQuery() > 0; } catch (SqlException) { return false; } catch (InvalidOperationException) { return false; } catch { return false; } finally { conn.Close(); } } /// <summary> /// 执行一个带参存储过程 /// </summary> /// <param name="procName">存储过程名</param> /// <param name="parameter">存储过程的输入参数</param> /// <returns>返回一个布尔类型的数据表示是否成功</returns> protected bool ExecuteProc(string procName, SqlParameter parameter) { comm = new SqlCommand(procName, conn); //指定操作类型(执行存储过程) comm.CommandType = CommandType.StoredProcedure; //添加存储过程的输入参数 comm.Parameters.Add(parameter); try { conn.Open(); return comm.ExecuteNonQuery() > 0; } catch (SqlException) { return false; } catch (InvalidOperationException) { return false; } catch { return false; } finally { conn.Close(); } } /// <summary> /// 执行一个带参存储过程 /// </summary> /// <param name="procName">存储过程名</param> /// <param name="parameters">存储过程的输入参数的数组</param> /// <returns>返回一个布尔类型的数据表示是否成功</returns> protected bool ExecuteProc(string procName, SqlParameter[] parameters) { comm = new SqlCommand(procName, conn); //指定操作类型(执行存储过程) comm.CommandType = CommandType.StoredProcedure; //添加存储过程的输入参数 foreach (SqlParameter parameter in parameters) { comm.Parameters.Add(parameter); } try { conn.Open(); return comm.ExecuteNonQuery() > 0; } catch (SqlException) { return false; } catch (InvalidOperationException) { return false; } catch { return false; } finally { conn.Close(); } } }}
DAL:
public class CategoryDAL:DBHelper { // 返回List表中List数据集 public List<Category> GetCategory() { List<Category> List = new List<Category>(); comm = new SqlCommand("select * from Category",conn); try { conn.Open(); DR = comm.ExecuteReader(); while (DR.Read()) { Category category = new Category(); category.CategoryId = Convert.ToInt32(DR["CategoryID"]); category.CategoryName = DR["categoryName"].ToString(); category.ParentId = Convert.ToInt32(DR["ParentId"]); List.Add(category); } return List; } catch (Exception) { throw; } finally { conn.Close(); } } // 获取类型名(没有子集) public List<Category> GetEndNodes() { List<Category> list = new List<Category>(); string sql = @"select CategoryName,CategoryId from Category where CategoryId not in (select ParentId from Category)"; comm = new SqlCommand(sql, conn); try { conn.Open(); DR = comm.ExecuteReader(); while (DR.Read()) { Category category= new Category(); category.CategoryId = Convert.ToInt32(DR["CategoryId"]); category.CategoryName = DR["CategoryName"].ToString(); list.Add(category); } return list; } catch { return null; } finally { conn.Close(); } } public class Category { private int categoryId; public int CategoryId { get { return categoryId; } set { categoryId = value; } } private string categoryName; public string CategoryName { get { return categoryName; } set { if (!String.IsNullOrEmpty(value)) { categoryName = value; } } } private int parentId; public int ParentId { get { return parentId; } set { parentId = value; } } } }
页面代码:
public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CategoryTreeBind(); } CategoryDAL dal = new CategoryDAL(); protected void CategoryTreeBind() { TreeNode root = new TreeNode("图书分类", "0"); this.treeCategory.Nodes.Add(root); List<WebApplication1.CategoryDAL.Category> categoryList = dal.GetCategory(); foreach (WebApplication1.CategoryDAL.Category model in categoryList) { if (model.ParentId.Equals(Int32.Parse(root.Value))) { TreeNode node1 = new TreeNode( model.CategoryName.ToString(), model.CategoryId.ToString() ); TreeNodeBind(node1, categoryList); root.ChildNodes.Add(node1); } } } private void TreeNodeBind(TreeNode node1, List<WebApplication1.CategoryDAL.Category> list) { foreach (WebApplication1.CategoryDAL.Category model in list) { if (model.ParentId.Equals(Int32.Parse(node1.Value))) { TreeNode node2 = new TreeNode( model.CategoryName.ToString(), model.CategoryId.ToString() ); TreeNodeBind(node2, list); //递归 node1.ChildNodes.Add(node2); } } } }
前台:
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <asp:treeview ID="treeCategory" ExpandDepth="1" runat="server"></asp:treeview> </div> </form></body></html>
- TreeView 用法
- treeview用法
- TreeView 用法
- TreeView用法
- treeview用法
- TreeView用法
- TreeView的用法
- TreeView基本用法
- TreeView的用法
- treeview的用法
- TreeView的用法
- treeview的用法
- Winform TreeView的用法
- treeview用法 2
- jquery treeview 的用法
- TreeView用法一
- C# TreeView的用法
- 掌握TreeView用法
- 169. Forbidden fruit is sweet. 禁果格外香
- C#缓存
- 王磊先生的-稳扎稳打 Silverlight 4.0 系列文章索引 教程
- Android_手把手教你写跳跃类游戏(1)
- business
- TreeView用法
- Android_手把手教你写跳跃类游戏(2)
- C语言学习笔记 (一) C语言的关键字
- Android_手把手教你写跳跃类游戏(3)
- DataGridView使用方法
- OpenGL版本与OpenGL扩展机制
- Android_手把手教你写跳跃类游戏——最终版本及所有源码!
- 控制台程序里调用控制台程序
- 毕业设计管理系统小结