Asp.net treeview实现无限级树实现代码
最近研究了一下treeview,发现有两种实现无限级树的方法,文字不想多写,直入主题。
先看看效果图:
先看看数据库表的设计,数据表主要包括ID,Name,ParentID这三项,其中ID是主键,ParentID对应节点的父节点:
方法一:用递归遍历数据,并将节点逐个添加到treeview中去。
1.先进行数据库连接和数据的读取,并将根节点先添加进treeview中,并利用递归getTreeView()实现数据的遍历和添加:
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeNode nodeCategory ;
connection conn = new connection();
List<Category> category = conn.getCategory();
Stack<Category> storeCategory = new Stack<Category>();
storeCategory.Push(category[0]);
nodeCategory = new TreeNode(category[0].Name.Trim(), category[0].Id.Trim());
TreeView1.Nodes.Add(nodeCategory);
getTreeView(storeCategory, category, nodeCategory);
}
}
2.数据遍历的递归函数,比较简单就不多说了。
复制代码 代码如下:
public void getTreeView(Stack<Category> categoryStack,List<Category> categoryList,TreeNode e)
{
Category tmp;
if(categoryStack.Count>0)
{
tmp=categoryStack.Pop();
for(int i=0;i<categoryList.Count;i++)
if(categoryList[i].ParentId.Trim()==tmp.Id.Trim())
{
categoryStack.Push(categoryList[i]);
TreeNode childNote = new TreeNode(categoryList[i].Name.Trim(), categoryList[i].Id.Trim());
e.ChildNodes.Add(childNote);
getTreeView(categoryStack, categoryList, childNote);
}
}
}
方法二:用TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)事件响应来逐个读取子节点。
1.第一步基本和上一方法的第一步一致,只是要将节点的设置为不展开。
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeNode nodeCategory ;
connection conn = new connection();
List<Category> category = conn.getCategory();
nodeCategory = new TreeNode(category[0].Name.Trim(), category[0].Id.Trim());
nodeCategory.PopulateOnDemand = true;
nodeCategory.Collapse();
相关文档:
ASP.NET 提供三种用于在出现错误时捕获和响应错误的主要方法:Page_Error 事件、Application_Error 事件以及应用程序配置文件 (Web.config)。
如果您不调用 Server.ClearError 或者捕获 Page_Error 或 Application_Error 事件中的错误,则将根据 Web.config 文件的 <customErrors> 部分中的设置处理错误。在 <cus ......
Windows 身份验证提供程序 提供有关如何将 Windows 身份验证与 Microsoft Internet 信息服务 (IIS) 身份验证结合使用来确保 ASP.NET 应用程序安全的信息。 Forms 身份验证提供程序 提供有关如何使用您自己的代码创建应用程序特定的登录窗体并执行身份验证的信息。使用 Forms 身份验证的一种简便方法是使用 ASP.NET 成员资格 ......
using System;
using System.Collections.Generic;
using System.Text;
namespace Common
{
/// <summary>
/// 转换人民币大小金额。
/// </summary>
public class Rmb
{
/// <summary>
/// 转换人民币大小金额
/// </summary>
/// <param name="num">金额</ ......
Forms身份验证用来判断是否合法用户,当用户合法后,再通过用户的角色决定能访问的页面。
主要思想:Forms身份验证用来判断是否合法用户,当用户合法后,再通过用户的角色决定能访问的页面。
具体步骤:
1、创建一个网站,结构如下:
网站根目录
Admin目录 ----> 管理员目录
Manager.aspx ----> 管理员可 ......