<SCRIPT type=text/javascript> var tree = new tree(); var root = new root(1,"欢迎使用OA帮助中心","right.htm","PageFrame"); tree.addRoot(root); tree.addNode(new node(2,"OA首页使用",1,"file","HomePage.htm","PageFrame"));<% string Title=""; string SubTitle=""; int itemNo=3; int itemNoUp=dtMenu.Rows.Count+itemNo; for(int i=0;i<dtMenu.Rows.Count;i++,itemNo++) { Title=dtMenu.Rows[i][2].ToString(); %> tree.addNode(new node(<%=itemNo%>,'<%=Title%>',1,"folder","#","")); <% GetChildItem(dtMenu.Rows[i][0].ToString()); for(int j=0;j<intCount;j++,itemNoUp++) { SubTitle=dtChildMenu.Rows[j][2].ToString(); %> tree.addNode(new node(<%=itemNoUp%>,'<%=SubTitle%>',<%=itemNo%>,"file",'<%=dtChildMenu.Rows[j][5].ToString()%>',"PageFrame")); <% } } %> tree.drawRoot(); tree.drawNodes(tree.root); </SCRIPT> </TD></TR></TBODY></TABLE></CENTER> <SCRIPT language=JavaScript> <!-- hide function goHist(a) { history.go(a); } //--> </SCRIPT> </BODY>
后台: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;using OA.ADO;namespace OA.Help { /// <summary> /// 功能: 菜单显示 /// 主要函数:BindMenu;GetChildItem /// 作者: iceguy /// 日期: 2005-9-6 /// </summary> public class LeftMenu : OA.BasePage { public DataTable dtMenu; public DataTable dtChildMenu; public int intCount; AccessDB adb = new AccessDB(); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 this.PageBegin("HomePage"); if(!IsPostBack) BindMenu(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
最好是vs2005的。。因为2003转换的时候老出错。[email protected]
{
DataTable dt = 数据源; //自己去指定
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID is null";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["Name"].ToString();
node.Value = drv["ID"].ToString();
node.ImageUrl = "~/images/folder.GIF";
node.Expanded = true;
this.tvRight.Nodes.Add(node);
AddReplies(dt,node);
}
}//递归函数
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row["Name"].ToString();
replyNode.Value = row["ID"].ToString();
replyNode.Expanded = false;
node.ChildNodes.Add(replyNode);
AddReplies(dt,replyNode);
}
}把你的表里加个ParentID字段就行了
加到哪个表里面,他有什么作用啊
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;public partial class xuexi_treeview : System.Web.UI.Page
{
protected SqlConnection Scon;
protected SqlDataAdapter Sda;
protected DataSet Ds;
protected void Page_Load(object sender, EventArgs e)
{
Scon = new SqlConnection("server=.;uid=sa;pwd=sa;database=Fzfs315net");
Scon.Open();
Sda = new SqlDataAdapter("select * from treeview",Scon);
Ds = new DataSet();
Sda.Fill(Ds); }
private void BindTree()
{
DataTable dt = Ds.Tables[0]; //自己去指定
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID is null";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["Name"].ToString();
node.Value = drv["ID"].ToString();
//node.ImageUrl = "~/images/folder.GIF";
node.Expanded = true;
this.tvRight.Nodes.Add(node);
AddReplies(dt, node);
} }
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row["Name"].ToString();
replyNode.Value = row["ID"].ToString();
replyNode.Expanded = false;
node.ChildNodes.Add(replyNode);
AddReplies(dt, replyNode);
}
}}
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" bgcolor=GhostWhite>
<table border="0" cellpadding=0 cellspacing=0 width="100%">
<tr><td colspan="2" height="3"></td></tr>
<tr>
<td width="3"></td>
<td>
<!--
--制作人:iceguy
--制作内容:JS树形目录
--制作时间:2005-12-20
-->
<SCRIPT type=text/javascript>
var tree = new tree();
var root = new root(1,"欢迎使用OA帮助中心","right.htm","PageFrame");
tree.addRoot(root);
tree.addNode(new node(2,"OA首页使用",1,"file","HomePage.htm","PageFrame"));<%
string Title="";
string SubTitle="";
int itemNo=3;
int itemNoUp=dtMenu.Rows.Count+itemNo;
for(int i=0;i<dtMenu.Rows.Count;i++,itemNo++)
{
Title=dtMenu.Rows[i][2].ToString();
%>
tree.addNode(new node(<%=itemNo%>,'<%=Title%>',1,"folder","#",""));
<%
GetChildItem(dtMenu.Rows[i][0].ToString());
for(int j=0;j<intCount;j++,itemNoUp++)
{
SubTitle=dtChildMenu.Rows[j][2].ToString();
%>
tree.addNode(new node(<%=itemNoUp%>,'<%=SubTitle%>',<%=itemNo%>,"file",'<%=dtChildMenu.Rows[j][5].ToString()%>',"PageFrame"));
<%
}
}
%>
tree.drawRoot();
tree.drawNodes(tree.root);
</SCRIPT>
</TD></TR></TBODY></TABLE></CENTER>
<SCRIPT language=JavaScript>
<!-- hide
function goHist(a)
{
history.go(a);
}
//-->
</SCRIPT>
</BODY>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;using OA.ADO;namespace OA.Help
{
/// <summary>
/// 功能: 菜单显示
/// 主要函数:BindMenu;GetChildItem
/// 作者: iceguy
/// 日期: 2005-9-6
/// </summary>
public class LeftMenu : OA.BasePage
{
public DataTable dtMenu;
public DataTable dtChildMenu;
public int intCount;
AccessDB adb = new AccessDB();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.PageBegin("HomePage");
if(!IsPostBack) BindMenu();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion /// <summary>
/// 菜单标题初始化
/// </summary>
private void BindMenu()
{
SqlParameter[] pamTitle = { new SqlParameter("@chrStaffNo",SqlDbType.Char,5,ParameterDirection.Input,true,0,0,null,DataRowVersion.Current,"")};
DataSet ds = adb.GetDataSet("sysHelpMainItem",pamTitle);
dtMenu = ds.Tables[0];
}
/// <summary>
/// 获取子菜单信息
/// </summary>
/// <param name="strItemNo">栏目代码</param>
public void GetChildItem(string strItemNo)
{
SqlParameter[] pamItemNo = {
new SqlParameter("@chrStaffNo",SqlDbType.Char,5,ParameterDirection.Input,true,0,0,null,DataRowVersion.Current,""),
new SqlParameter("@varItemNo",SqlDbType.VarChar,50,ParameterDirection.Input,true,0,0,null,DataRowVersion.Current,strItemNo)
};
DataSet ds = adb.GetDataSet("sysHelpChildItem",pamItemNo);
dtChildMenu = ds.Tables[0];
intCount = dtChildMenu.Rows.Count;
}
}
}
///制作人:iceguy
///日期:2005-12-23
///内容:JS树形目录
///
///
///
///
imageDir = "images/";
/** 文件节点的图片 */
img_file = imageDir+"file.gif";
/** 关闭的目录的图片 */
img_folder_close = imageDir+"close.gif";
/** 在中间的加号的图片 */
img_plus = imageDir+"plusnode.gif";
/** 在树末尾的加号的图片 */
img_plus_last = imageDir+"pluslastnode.gif";
/** 打开的目录的图片 */
img_folder_open = imageDir+"open.gif";
/** 在树末尾的减号的图片 */
img_minus_last = imageDir+"minuslastnode.gif";
/** 在中间的减号的图片 */
img_minus = imageDir+"minusnode.gif";
/** 无节点线条的图片 */
img_line = imageDir+"line.gif";
/** 最后节点的线条图片 */
img_line_last = imageDir+"lastnode.gif";
/** 中间节点的线条图片 */
img_line_mid = imageDir+"node.gif";
/** 空白区域的图片 */
img_blank = imageDir+"blank.gif";
function tree(){/** 根节点 */
this.root = null;
/** 节点个数 */
this.length = 0;
/** 节点数组 */
this.nodes = new Array();
/** 在drawFrontLine时用来临时存储字符串 */
this.tempStr = "";/** 添加根节点 */
this.addRoot = addRoot;
/** 添加节点 */
this.addNode = addNode;/** 画出根节点 */
this.drawRoot = drawRoot;
/** 画出节点前的空白图片或连接线图片 */
this.drawFrontLine = drawFrontLine;
/** 画出节点 */
this.drawNode = drawNode;
/** 画出所有节点 */
this.drawNodes = drawNodes;
/** 得到节点的父节点 */
this.getParent = getParent;
/** 添加节点时,将同一层的其他节点的isLast属性设置为false */
this.setOtherIsLast = setOtherIsLast;
}/**
* 根节点对象
* @param id 根节点的id号
* @param name 根节点名称,显示在页面的连接的名字
* @param url 链接
* @param target 指示链接的目标页面
*/
function root(id,name,url,target) {
this.id = id;
this.name = name;
this.parentId = null;
this.type = "root";
this.url = url;
this.target = target;
}function addRoot(root) {
this.root = root;
this.length = 1;
this.nodes[0] = root;
}/**
* 节点对象
* @param id 节点id号
* @param name 节点名称,显示在页面上的链接的名字
* @param parentId 父节点id号
* @param type 节点的类型(folder|file)
* @param url 节点的链接
* @param target 节点链接的目标页面
*/function node(id,name,parentId,type,url,target) {
/** 节点id号 */
this.id = id;
/** 节点名称,显示在页面上的链接的名字 */
this.name =name;
/** 父节点id号 */
this.parentId = parentId;
/** 节点的类型(folder|file) */
this.type = type;
/** 节点的链接 */
this.url = url;
/** 节点链接的目标页面 */
this.target = target;
/** 节点的图片(目录或文件等) */
this.image = "";
/** 节点的前导图片(加号或减号或线条等) */
this.fImage = "";
/** 是否是同层中最后节点 */
this.isLast = false;
}/** 判断一个节点是否有父节点,如果有则返回其父节点,如果没有返回null */
function getParent(node) {
for (var i=0;i<this.length;i++)
{
if (this.nodes[i].id == node.parentId)
{
return this.nodes[i];
}
}
return null;
}/** 当添加一个新节点后,将与它处在同一层的其它元素的isLast标志设置为false */
function setOtherIsLast(node) {
for (var i=1;i<this.length;i++) //i=1,表示不包括根节点在内的循环
{
if (this.nodes[i].parentId == node.parentId && this.nodes[i].isLast) //如果找到父节点相同的,并且isLast为true的节点
{
this.nodes[i].isLast = false; //设置该节点的isLast为false
if (this.nodes[i].type == "folder") //设置图片为非末节点图片
{
this.nodes[i].fImage = img_plus;
} else {
this.nodes[i].fImage = img_line_mid;
}
return true;
}
}
return false;
}/** 为树的节点组nodes[]添加一个新的节点 */
function addNode(node) {
if (this.getParent(node) != null) //如果有父节点
{
this.setOtherIsLast(node); //设置同层中的其他元素为非末节点
node.isLast = true; //设置本节点为末节点
if (node.type == "folder") //根据节点类型设置图片
{
node.image = img_folder_close;
node.fImage = img_plus_last;
} else {
node.image = img_file;
node.fImage = img_line_last;
}
this.nodes[this.length] = node; //添加该节点到树的节点组nodes[]
this.length++; //节点数加1
} else {
alert("没有找到该节点的父节点,这是一个非法节点!");
}
}
/** 画出根节点 */
function drawRoot() {
document.write("<table border='0' cellspacing='0' cellpadding='0'>");
document.write("<tr style='font-size:12px'><td>");
document.write("<a onFocus='this.blur()' href='"+this.root.url+"' target='"+this.root.target+"'><img border='0' src='"+img_folder_close+"'></a>");
document.write("</td><td valign='middle'>");
document.write("<a onFocus='this.blur()' href='"+this.root.url+"' target='"+this.root.target+"'>"+this.root.name+"</a>");
document.write("</td></tr>");
document.write("</table>");
}/** 画出节点 */
function drawNode(node) {
document.write("<table border='0' cellspacing='0' cellpadding='0'>");
document.write("<tr style='font-size:12px'><td>");
this.drawFrontLine(node);
if (node.type == "folder")
{
document.write("<a onClick='clickOnFolder(0)'><img border='0' src='"+node.fImage+"'></a>");
document.write("<a onClick='clickOnFolder(2)' style='cursor:hand;'><img border='0' src='"+node.image+"'></a>");
document.write("</td><td valign='middle'>");
document.write("<span onClick='clickOnFolder(4)' onFocus='this.blur()' style='cursor:hand;' onMouseOut=this.style.color='#000000' onMouseOver=this.style.color='#FF0000'>"+node.name+"</span>");
} else {
document.write("<img border='0' src='"+node.fImage+"'>");
document.write("<a onFocus='this.blur()' href='"+node.url+"' target='"+node.target+"'><img border='0' src='"+node.image+"'></a>");
document.write("</td><td valign='middle'>");
document.write("<a onFocus='this.blur()' href='"+node.url+"' target='"+node.target+"'>"+node.name+"</a>");
}
document.write("</td></tr>");
document.write("</table>");
}/** 画出整个树的节点组 */
function drawNodes(node) {
if (node.type != "root")
{
document.write("<div style='display:none'>");
}
for (var i=1;i<this.length;i++)
{
if (this.nodes[i].parentId!=null && this.nodes[i].parentId == node.id)
{
this.drawNode(this.nodes[i]); //画出节点
this.drawNodes(this.nodes[i]); //递归画出整个节点组的节点
}
}
if (node.type != "root")
{
document.write("</div>");
}
}/** 画出节点前的前导图片,有空格或线条图片.
如果其父节点是一个末节点,那么该对应列的前导图片为空格图片.
如果不是末节点,应该添加线条图片.
这里进行了递归运算,但由于其图片顺序为反顺序,所以设置一个tempStr来暂存需要画出的图片,
用以保证其图片顺序为正确顺序. */
function drawFrontLine(node) {
var tempStr = "";
for (var i=1;i<this.length;i++)
{
if (this.nodes[i].id == node.parentId)
{
if (this.nodes[i].isLast)
{
tempStr = "<img src='"+img_blank+"'>" + tempStr;;
} else {
tempStr = "<img src='"+img_line+"'>" + tempStr;
}
this.drawFrontLine(this.nodes[i]);
}
}
document.write(tempStr);
}
/** 当点击目录节点的前导"加号|减号"或"文件夹"图片或者"目录名称"时,展开层或收缩层,并用相应的图片替代现有图片,实现动态收缩动作 */
function clickOnFolder(index) {
var srcIndex = event.srcElement.sourceIndex;
var divElement = document.all[srcIndex+5-index]; //得到层对象
var imgElement = document.all[srcIndex+2-index]; //得到图片对象
var fimgElement = document.all[srcIndex-index]; //得到前导图片对象(即它本身)
if (divElement.style.display == "none")
{
divElement.style.display = "";
imgElement.src=img_folder_open;;
if (fimgElement.src!=null && fimgElement.src.indexOf(img_plus)!=-1)
{
fimgElement.src=img_minus;
} else {
fimgElement.src=img_minus_last;
}
} else {
divElement.style.display = "none";
imgElement.src=img_folder_close;
if (fimgElement.src!=null && fimgElement.src.indexOf(img_minus)!=-1)
{
fimgElement.src=img_plus;;
} else {
fimgElement.src=img_plus_last;
}
}
}
{
BindTreeView1();// 在此处放置用户代码以初始化页面
}
public void BindTreeView1()
{
SqlConnection conn=new SqlConnection(strconn);
conn.Open();
try
{
SqlDataAdapter da=new SqlDataAdapter("select * from kclist",conn);
DataSet ds=new DataSet();
da.Fill(ds);
this.ViewState["ds"]=ds;
}
catch(Exception ex)
{
throw (ex);
}
finally
{
conn.Close();
}
AddTree(0,(TreeNode)null);
} #region BindTreeView1()
public void AddTree(int ParentID,TreeNode pNode)
{
DataSet ds=(DataSet)this.ViewState["ds"];
DataView dvTree=new DataView(ds.Tables[0]);
dvTree.RowFilter="[ParentID]="+ParentID;
foreach(DataRowView row in dvTree)
{
TreeNode Node=new TreeNode();
if(pNode==null)
{
Node.Text=row["Context"].ToString(); TreeView1.Nodes.Add(Node);
Node.Expanded=true;
AddTree(int.Parse(row["ID"].ToString()),Node);
}
else
{
Node.Text=row["Context"].ToString(); Node.NavigateUrl="kclist.aspx?id=" +HttpUtility.UrlEncode(row["Context"].ToString());
Node.Target="main"; pNode.Nodes.Add(Node);
Node.Expanded=true;
AddTree(int.Parse(row["ID"].ToString()),Node);
}
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
if(Session["userId"]==null)
{
//Response.Write("登陆已超时,请重新登陆!");
}
else
{
DataSet ds;
ds=menu.MenuInfo(Session["userId"].ToString());
buildTree(tvMenu,ds);
}
}
public bool buildTree(Microsoft.Web.UI.WebControls.TreeView tvMenu,DataSet ds )
{
//清空所有节点
tvMenu.Nodes.Clear(); //如果为最高级,则修改以适应ds的relation中的关系
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if(dbRow["ParentMenu"].ToString()=="0")
{
dbRow["ParentMenu"]=DBNull.Value;
}
}
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["MenuID"], ds.Tables[0].Columns["ParentMenu"]);
//循环绑定父node
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if(dbRow.IsNull("ParentMenu"))
{
Microsoft.Web.UI.WebControls.TreeNode newNode = CreateNode(dbRow["MenuName"].ToString(),dbRow["NavigateUrl"].ToString(),dbRow["ImageUrl"].ToString());
tvMenu.Nodes.Add(newNode);
PopulateSubTree(dbRow, newNode);
}
}
return true; }
/// <summary>
/// 循环绑定子node
/// </summary>
/// <param name="dbRow">dataset的行</param>
/// <param name="node">需要添加的节点</param>
private void PopulateSubTree(DataRow dbRow, Microsoft.Web.UI.WebControls.TreeNode node)
{
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
Microsoft.Web.UI.WebControls.TreeNode childNode = CreateNode(childRow["MenuName"].ToString(),childRow["NavigateUrl"].ToString(),childRow["ImageUrl"].ToString());
node.Nodes.Add(childNode);
PopulateSubTree(childRow, childNode);
}
}
private Microsoft.Web.UI.WebControls.TreeNode CreateNode(string text,string NavigateUrl,string imageurl)
{
Microsoft.Web.UI.WebControls.TreeNode node = new Microsoft.Web.UI.WebControls.TreeNode();
/*
* 其他node属性自己添加,现在只添加两个
node.ID;
node.ImageUrl;
node.Target;
node.Expanded;
node.CheckBox
* */
node.ImageUrl=imageurl;
node.Text = "<font color='#ffffff'>"+text+"</font>";
node.NavigateUrl=NavigateUrl;
node.Target = "MainFrame";
//node.t
return node;
你的方法能读出数据,但是没有收缩的效果,父节点之后子节点和父节点越点越多