使用treeview控件读数据库动态生成带checkbox复选框的树形导航菜单asp...
发布网友
发布时间:2022-05-06 16:42
我来回答
共3个回答
热心网友
时间:2022-04-20 20:50
页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tree.aspx.cs" Inherits="MyTeachers.web.Tree" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" Height="257px" ImageSet="WindowsHelp"
Width="142px" Target="rightFrame">
</asp:TreeView>
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
namespace MyTeachers.web
{
public partial class Tree : System.Web.UI.Page
{
DataView dv;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int id =Convert.ToInt32(Session["userID"]);
//数据库执行查询的方法就不写了。
//Operator op = new Operator();
//这里执行的是:select * from tabmenu
dt = BLL.tabMenuBLL.GetAlltabMenut(id);
//第一次加载时调用方法传参
CreateTree(0, null, dt, this.TreeView1);
}
}
/// <summary>
/// 创建一个树
/// </summary>
/// <param name="parentID">父ID</param>
/// <param name="node">节点</param>
/// <param name="dt">DataTable</param>
/// <param name="treeView">TreeView的名称</param>
public void CreateTree(int parentID, TreeNode node, DataTable dt, TreeView treeView)
{
//实例化一个DataView dt = 传入的DataTable
dv = new DataView(dt);
//筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID)
dv.RowFilter = "[pid]=" + parentID;
//用foreach遍历dv
foreach (DataRowView row in dv)
{
//第一次加载时为空
if (node == null)
{
//创建根节点
TreeNode root = new TreeNode();
//root.Target = "rightFrame";
//必须与数据库的对应
root.Text = row["menuName"].ToString();
root.Value = row["menuID"].ToString();
//添加根节点
this.TreeView1.Nodes.Add(root);
//递归调用方法本身
CreateTree(int.Parse(row["menuID"].ToString()), root, dt, treeView);
}
else
{
//添加子节点
TreeNode childNode = new TreeNode();
childNode.Text = row["menuName"].ToString();
childNode.Value = row["menuID"].ToString();
childNode.NavigateUrl = row["menuUrl"].ToString();
node.ChildNodes.Add(childNode);
CreateTree(int.Parse(row["menuID"].ToString()), childNode, dt, treeView);
}
}
}
}
}
热心网友
时间:2022-04-20 22:08
我给你的资料不给我100分,是太不够意思,微软的.net控件详解,没有那个比这个更权威的了,我就是看这个出来的。
网址:
热心网友
时间:2022-04-21 01:34
加QQ347832281我发给你源码
private void Bind()
{
string rootStr = "维护";
TreeNode rootNode = new TreeNode(rootStr);
rootNode.Value = "-1";
rootNode.Target = "RightDown";
rootNode.NavigateUrl = "#";
//PopulateTreeView(0, rootNode);
PopulateTreeView1(0, rootNode);
this.tvType.Nodes.Add(rootNode);
}
/// <summary>
/// 最简单的无限级绑定
/// </summary>
/// <param name="parentId"></param>
/// <param name="parentNode"></param>
private void PopulateTreeView1(int parentId, TreeNode parentNode)
{
DataView dv = BySqlHelperQuery.GetClass1("pid=" + parentId.ToString()).DefaultView;
foreach (DataRowView drv in dv)
{
TreeNode myNode = new TreeNode(drv["Name"].ToString());
//myNode.Expanded = false;
myNode.Value = drv["Id"].ToString();
myNode.Target = "RightDown";
//myNode.NavigateUrl = "body.aspx?classId=" + drv["Id"].ToString();
myNode.NavigateUrl = "#";
parentNode.ChildNodes.Add(myNode);
PopulateTreeView1(Convert.ToInt32(drv["Id"].ToString()), myNode);
}
}
js 如下:
//实现你说的checkbox效果
function client_OnTreeNodeChecked(event)
{
//得到当前所 Click 的对象
var objNode;
if(!public_IsObjectNull(event.srcElement))
{
//IE
objNode = event.srcElement;
}
else
{
//FF
objNode = event.target;
}
//判断是否 Click 的 CheckBox
if(!public_IsCheckBox(objNode))
return;
var objCheckBox = objNode;
//根据CheckBox状态进行相应处理
if(objCheckBox.checked==true)
{
//递归选中父节点的 CheckBox
setParentChecked(objCheckBox);
//递归选中所有的子节点
setChildChecked(objCheckBox);
}
else
{
//递归取消选中所有的子节点
setChildUnChecked(objCheckBox);
//递归取消选中父节点(如果当前节点的所有其他同级节点也都未被选中).
setParentUnChecked(objCheckBox);
}
}
//判断对象是否为空
function public_IsObjectNull(element)
{
if(element==null || element == "undefined")
return true;
else
return false;
}
//判断对象是否为 CheckBox
function public_IsCheckBox(element)
{
if(public_IsObjectNull(element))
return false;
if(element.tagName!="INPUT" || element.type!="checkbox")
return false;
else
return true;
}
//得到包含所有子节点的 Node(Div 对象)
function public_CheckBox2Node(element)
{
var objID = element.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("CheckBox"));
return document.getElementById(objID+"Nodes");
}
//得到父节点的 CheckBox
function public_Node2CheckBox(element)
{
var objID = element.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("Nodes"));
return document.getElementById(objID+"CheckBox");
}
//得到本节点所在的 Node(Div 对象)
function public_GetParentNode(element)
{
var parent = element.parentNode;
var upperTagName = "DIV";
//如果这个元素还不是想要的 tag 就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//设置节点的父节点 Checked
function setParentChecked(currCheckBox)
{
var objParentNode= public_GetParentNode(currCheckBox);
if(public_IsObjectNull(objParentNode))
return;
var objParentCheckBox = public_Node2CheckBox(objParentNode);
if(!public_IsCheckBox(objParentCheckBox))
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
//当父节点的所有子节点都未被选中时,设置父节点 UnChecked
function setParentUnChecked(currCheckBox)
{
var objParentNode= public_GetParentNode(currCheckBox);
if(public_IsObjectNull(objParentNode))
return;
//判断 currCheckBox 的同级节点是否都为 UnChecked.
if(!IsMyChildCheckBoxsUnChecked(objParentNode))
return;
var objParentCheckBox = public_Node2CheckBox(objParentNode);
if(!public_IsCheckBox(objParentCheckBox))
return;
objParentCheckBox.checked = false;
setParentUnChecked(objParentCheckBox);
}
//设置节点的子节点 UnChecked
function setChildUnChecked(currObj)
{
var currNode;
if(public_IsCheckBox(currObj))
{
currNode = public_CheckBox2Node(currObj);
if (public_IsObjectNull(currNode))
return;
}
else
currNode = currObj;
var currNodeChilds = currNode.childNodes;
var count = currNodeChilds.length;
for(var i=0;i<count;i++)
{
var childCheckBox = currNodeChilds[i];
if(public_IsCheckBox(childCheckBox))
{
childCheckBox.checked = false;
}
setChildUnChecked(childCheckBox);
}
}
//设置节点的子节点 Checked
function setChildChecked(currObj)
{
var currNode;
if(public_IsCheckBox(currObj))
{
currNode = public_CheckBox2Node(currObj);
if (public_IsObjectNull(currNode))
return;
}
else
currNode = currObj;
var currNodeChilds = currNode.childNodes;
var count = currNodeChilds.length;
for(var i=0;i<count;i++)
{
var childCheckBox = currNodeChilds[i];
if(public_IsCheckBox(childCheckBox))
{
childCheckBox.checked = true;
}
setChildChecked(childCheckBox);
}
}
//判断该节点的子节点是否都为 UnChecked
function IsMyChildCheckBoxsUnChecked(currObj)
{
var retVal = true;
var currNode;
if(public_IsCheckBox(currObj) && currObj.checked == true)
{
return false;
}
else
currNode = currObj;
var currNodeChilds = currNode.childNodes;
var count = currNodeChilds.length;
for(var i=0;i<count;i++)
{
if (retVal == false)
break;
var childCheckBox = currNodeChilds[i];
if(public_IsCheckBox(childCheckBox) && childCheckBox.checked == true)
{
retVal = false;
return retVal;
}
else
retVal = IsMyChildCheckBoxsUnChecked(childCheckBox);
}
return retVal;
}追问你的这个回答看起来挺好的,可是我不怎么会用js,所以我借用了楼下的C#版本修改成功了。依然非常感谢您的回答哦~