当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 常用的在数据库中建立无限级树形菜单的asp.net代码

ASP.NET
c# Random快速连续产生相同随机数的解决方案
form身份验证通过后,只能用FormsAuthentication.RedirectFromLoginPage
ASP.NET Global.asax应用程序文件简介
Asp.net下载功能的解决方案代码
asp.net 生成数字和字母组合的随机数
asp.net显示页面执行时间
DiscuzNT 论坛与主站的同步登录与退出
c# 读取Northwind数据库image字段
asp.net 数据访问层基类
C# 文件保存到数据库中或者从数据库中读取文件
ASP.NET 防止用户跳过登陆界面
asp.net 字符串加密解密技术
asp.net 票据简单应用
基于C# 网站地图制作
asp.net GridView 中增加记录的方法
asp.net 自动将汉字转换成拼音第一个字母
运行asp.net时出现 http错误404-文件或目录未找到
System.Runtime.InteropServices.COMException的解决方法
VS2005 180天限制破解方法
asp.net 面试+笔试题目

ASP.NET 中的 常用的在数据库中建立无限级树形菜单的asp.net代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 113 ::
收藏到网摘: n/a

经常在项目中遇到建立无限级树形菜单展示的效果,这里简单地做了一个,基本后台代码如下
复制代码 代码如下:

private DataTable GetTable(int topid)
{
DataTable dt = null;
try
{
string constr = "server=.;database=tqnpc;uid=sa;pwd=sa";
string selstr = "select * from RW_工作关系 where main_id=" + topid + "";
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter da = new SqlDataAdapter(selstr, con);
dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return dt;
}
protected void MakeTree()
{
DataTable dt = GetTable(0);
try
{
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["MAIN_ID"].ToString();
tn.Value = dt.Rows[i]["REF_ID"].ToString();
tn.SelectAction = TreeNodeSelectAction.Select;
TreeView1.Nodes.Add(tn);
AddTreeNodes(int.Parse(dt.Rows[i]["REF_ID"].ToString()), int.Parse(dt.Rows[i]["REF_ID"].ToString()), tn);
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

这个方法对数据库的结构也有一定的要求,数据库的设计如下: