使用TreeView,都是checkbox结构,如何选择父节点后使子节点也自动选上
使用TreeView都是checkbox结构
如何选择父节点后使子节点也自动选上 --------------------编程问答-------------------- 遍历子节点喽 --------------------编程问答-------------------- private void treeView1_Check(object sender, TreeViewEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
foreach (TreeNode node in e.Node.Nodes)
{
node.Checked = e.Node.Checked;
}
}
} --------------------编程问答-------------------- 用递归在AfterCheck中加上selectnode(e.Node,e.Node.Checked)
Private Sub selectnode(ByVal n As TreeNode, ByVal ck As Boolean)
If Not (n.Nodes.Count = 0) Then
n = n.LastNode
While Not (n Is Nothing)
n.Checked = ck
selectnode(n, ck)
n = n.PrevNode
End While
End If
End Sub --------------------编程问答-------------------- 这个必须你在服务器端自行写事件处理来完成,没有自动化的实现。 --------------------编程问答-------------------- 服务器:
TreeView1.Attributes.Add("OnClick", "OnTreeNodeChecked(event)");
客户端脚本如下:
/***************************************************************************************
Name: Client Javascript for ASP.NET 2.0 TreeView
Description: ASP.NET 2.0 TreeView lack for client operation. This set of functions provide
some supports. Includes:
* get node
* change checkbox status of parent and child nodes
Author: Zhangtao, zhangtao.it@gmail.com
Date: 2006-03-30
Commonts:
***************************************************************************************/
function TV2_SetChildNodesCheckStatus(node,isChecked)
{
var childNodes = TV2i_GetChildNodesDiv(node);
if(childNodes == null)
return;
var inputs = WebForm_GetElementsByTagName(childNodes,"INPUT");
if(inputs == null || inputs.length == 0)
return;
for(var i = 0; i < inputs.length; i++)
{
if(IsCheckBox(inputs[i]))
inputs[i].checked = isChecked;
}
}
function TV2_NodeOnChildNodeCheckedChanged(tree,node,isChecked)
{
if(node == null)
return;
var childNodes = TV2_GetChildNodes(tree,node);
if(childNodes == null || childNodes.length == 0)
return;
var isAllSame = true;
for(var i = 0; i < childNodes.length; i++)
{
var item = childNodes[i];
var value = TV2_NodeGetChecked(item);
if(isChecked != value)
{
isAllSame = false;
break;
}
}
var parent = TV2_GetParentNode(tree,node);
if(isAllSame)
{
TV2_NodeSetChecked(node,isChecked);
TV2_NodeOnChildNodeCheckedChanged(tree,parent,isChecked);
}
else
{
TV2_NodeSetChecked(node,false);
TV2_NodeOnChildNodeCheckedChanged(tree,parent,false);
}
}
function TV2_GetNode(tree,element)
{
var id = element.id.replace(tree.id,"");
id = id.replace(element.type,"");
id = tree.id + id;
var node = document.getElementById(id);
if(node == null) //leaf node, no "A" node
return element;
return node;
}
function TV2_GetParentNode(tree,node)
{
var div = WebForm_GetParentByTagName(node,"DIV");
//The structure of node: <table>information of node</table><div>child nodes</div>
var table = div.previousSibling;
if(table == null)
return null;
return TV2i_GetNodeInElement(tree,table);
}
function TV2_GetChildNodes(tree,node)
{
if(TV2_NodeIsLeaf(node))
return null;
var children = new Array();
var div = TV2i_GetChildNodesDiv(node);
var index = 0;
for(var i = 0; i < div.childNodes.length; i++)
{
var element = div.childNodes[i];
if(element.tagName != "TABLE")
continue;
var child = TV2i_GetNodeInElement(tree,element);
if(child != null)
children[index++] = child;
}
return children;
}
function TV2_NodeIsLeaf(node)
{
return !(node.tagName == "A"); //Todo
}
function TV2_NodeGetChecked(node)
{
var checkbox = TV2i_NodeGetCheckBox(node);
return checkbox.checked;
}
function TV2_NodeSetChecked(node,isChecked)
{
var checkbox = TV2i_NodeGetCheckBox(node);
if(checkbox != null)
checkbox.checked = isChecked;
}
function IsCheckBox(element)
{
if(element == null)
return false;
return (element.tagName == "INPUT" && element.type.toLowerCase() == "checkbox");
}
function TV2_GetTreeById(id)
{
return document.getElementById(id);
}
function TV2i_GetChildNodesDiv(node)
{
if(TV2_NodeIsLeaf(node))
return null;
var childNodsDivId = node.id + "Nodes";
return document.getElementById( childNodsDivId );
}
function TV2i_GetNodeInElement(tree,element)
{
var node = TV2i_GetNodeInElementA(tree,element);
if(node == null)
{
node = TV2i_GetNodeInElementInput(tree,element);
}
return node;
}
function TV2i_GetNodeInElementA(tree,element)
{
var as = WebForm_GetElementsByTagName(element,"A");
if(as== null || as.length == 0)
return null;
var regexp = new RegExp("^" + tree.id + "n\\d+$");
for(var i = 0; i < as.length; i++)
{
if(as[i].id.match(regexp))
{
return as[i];
}
}
return null;
}
function TV2i_GetNodeInElementInput(tree,element)
{
var as = WebForm_GetElementsByTagName(element,"INPUT");
if(as== null || as.length == 0)
return null;
var regexp = new RegExp("^" + tree.id + "n\\d+");
for(var i = 0; i < as.length; i++)
{
if(as[i].id.match(regexp))
{
return as[i];
}
}
return null;
}
function TV2i_NodeGetCheckBox(node)
{
if(IsCheckBox(node))
return node;
var id = node.id + "CheckBox";
return document.getElementById(id);
}
function OnTreeNodeChecked()
{
var ele = event.srcElement;
if(ele.type=='checkbox')
{
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
if(div==null)return;
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
checkBoxs[i].checked=ele.checked;
}
}
} --------------------编程问答-------------------- 遍历,判断,处理,LS js强 --------------------编程问答-------------------- 二楼的不行,,你那样只能选择第二级的菜单,,第易做图以后的都不会选到
//下面这个方法是树的点击事件
private void TreeView_AfterCheck(object sender, TreeViewEventArgs e)
{
if(e.Action!=TreeViewAction.Unknown)
{
if(e.Node.Nodes.Count>0)
{
pCheckAllChildNodes(e.Node,e.Node.Checked);
}
pCheckParentNodes(e.Node);
}
}
//选择父节点
private void pCheckParentNodes(TreeNode treeNode)
{
TreeNode _oParentNode=treeNode.Parent;
while(_oParentNode!=null)
{
bool _bChecked=false;
for(int _k=0;_k<_oParentNode.Nodes.Count;_k++)
{
if(_oParentNode.Nodes[_k].Checked!=_bChecked)
{
_bChecked=!_bChecked;
break;
}
}
_oParentNode.Checked=_bChecked;
_oParentNode=_oParentNode.Parent;
}
}
//选择子节点
private void pCheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
pCheckAllChildNodes(node, nodeChecked);
}
}
}
补充:.NET技术 , ASP.NET