1. asp net2.0 TreeView 客户端按需展开,其实msdn里就有,copy出来看着方便点
设置节点的PopulateOnDemand="True" SelectAction="Expand"属性
后台treeView TreeNodePopulate事件:
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.Value == "9") return;
TreeNode NewNode = new TreeNode(e.Node.Value, (int.Parse(e.Node.Value)+1).ToString());
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
NewNode.PopulateOnDemand = true;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
e.Node.ChildNodes.Add(NewNode);
}
{
if (e.Node.Value == "9") return;
TreeNode NewNode = new TreeNode(e.Node.Value, (int.Parse(e.Node.Value)+1).ToString());
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
NewNode.PopulateOnDemand = true;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
e.Node.ChildNodes.Add(NewNode);
}
2. TreeView 客户端全选
通过js实现的选择母节点check后,所有字节点也选中
<script language="javascript" type="text/javascript">
function client_OnTreeNodeChecked()
{
var obj = window.event.srcElement;
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox")
{
var treeNode = obj;
checkedState = treeNode.checked;
do
{
obj = obj.parentElement;
}while (obj.tagName != "TABLE")
var parentTreeLevel = obj.rows[0].cells.length;
var parentTreeNode = obj.rows[0].cells[0];
var tables = obj.parentElement.getElementsByTagName("TABLE");
var numTables = tables.length
if (numTables >= 1)
{
for (i=0; i < numTables; i++)
{
if (tables[i] == obj)
{
treeNodeFound = true;
i++;
if (i == numTables)
{
return;
}
}
if (treeNodeFound == true)
{
var childTreeLevel = tables[i].rows[0].cells.length;
if (childTreeLevel > parentTreeLevel)
{
var cell = tables[i].rows[0].cells[childTreeLevel - 1];
var inputs = cell.getElementsByTagName("INPUT");
inputs[0].checked = checkedState;
}
else
{
return;
}
}
}
}
}
}
</script>
function client_OnTreeNodeChecked()
{
var obj = window.event.srcElement;
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox")
{
var treeNode = obj;
checkedState = treeNode.checked;
do
{
obj = obj.parentElement;
}while (obj.tagName != "TABLE")
var parentTreeLevel = obj.rows[0].cells.length;
var parentTreeNode = obj.rows[0].cells[0];
var tables = obj.parentElement.getElementsByTagName("TABLE");
var numTables = tables.length
if (numTables >= 1)
{
for (i=0; i < numTables; i++)
{
if (tables[i] == obj)
{
treeNodeFound = true;
i++;
if (i == numTables)
{
return;
}
}
if (treeNodeFound == true)
{
var childTreeLevel = tables[i].rows[0].cells.length;
if (childTreeLevel > parentTreeLevel)
{
var cell = tables[i].rows[0].cells[childTreeLevel - 1];
var inputs = cell.getElementsByTagName("INPUT");
inputs[0].checked = checkedState;
}
else
{
return;
}
}
}
}
}
}
</script>
使用:加入treeView客户端点击事件OnClick
<asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
ShowCheckBoxes="All" onclick="client_OnTreeNodeChecked();" >
结合第一条动态按需展开:
因js遍历时,按需获取的node在展开之前无法遍历到,
新展开的节点默认都没有选中
因此需要在动态展开时继承其母节点的选中状态
修改后台TreeView1_TreeNodePopulate事件
加入:
TreeNode NewNode = new TreeNode(e.Node.Value, (int.Parse(e.Node.Value) + 1).ToString());
if (e.Node.Checked == true) NewNode.Checked = true; //继承母节点选中状态