[原创]如何控制TreeView在打开的时候只展开两层?
树的Html代码如下:
别忘了在窗体的onload事件中调用ExpandOneTwo()函数
<body MS_POSITIONING="GridLayout" onload="ExpandOneTwo();">
![](/Images/OutliningIndicators/None.gif)
第1种非递归的方法:
第2种递归的方法:
1
<iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 320px; POSITION: absolute; TOP: 160px"
2
runat="server">
3
<iewc:TreeNode Text="Node0">
4
<iewc:TreeNode Text="Node3">
5
<iewc:TreeNode Text="Node9">
6
<iewc:TreeNode Text="Node0"></iewc:TreeNode>
7
<iewc:TreeNode Text="Node1"></iewc:TreeNode>
8
</iewc:TreeNode>
9
<iewc:TreeNode Text="Node10">
10
<iewc:TreeNode Text="Node2"></iewc:TreeNode>
11
<iewc:TreeNode Text="Node3"></iewc:TreeNode>
12
</iewc:TreeNode>
13
</iewc:TreeNode>
14
<iewc:TreeNode Text="Node4"></iewc:TreeNode>
15
</iewc:TreeNode>
16
<iewc:TreeNode Text="Node1">
17
<iewc:TreeNode Text="Node5">
18
<iewc:TreeNode Text="Node11">
19
<iewc:TreeNode Text="Node4"></iewc:TreeNode>
20
<iewc:TreeNode Text="Node5"></iewc:TreeNode>
21
</iewc:TreeNode>
22
<iewc:TreeNode Text="Node12"></iewc:TreeNode>
23
</iewc:TreeNode>
24
<iewc:TreeNode Text="Node6"></iewc:TreeNode>
25
</iewc:TreeNode>
26
<iewc:TreeNode Text="Node2">
27
<iewc:TreeNode Text="Node7">
28
<iewc:TreeNode Text="Node13">
29
<iewc:TreeNode Text="Node6"></iewc:TreeNode>
30
<iewc:TreeNode Text="Node7"></iewc:TreeNode>
31
</iewc:TreeNode>
32
<iewc:TreeNode Text="Node14"></iewc:TreeNode>
33
</iewc:TreeNode>
34
<iewc:TreeNode Text="Node8"></iewc:TreeNode>
35
</iewc:TreeNode>
36
</iewc:TreeView>
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/None.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
别忘了在窗体的onload事件中调用ExpandOneTwo()函数
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
第1种非递归的方法:
1
function ExpandOneTwo()
2
{
3
4
var NodeArray=new Array();
5
NodeArray=TreeView1.getChildren();
6
for(i=0;i<NodeArray.length;i++)
7
{
8
cNode=NodeArray[i];
9
cNode.setAttribute("expanded","true");
10
//alert(cNode.getAttribute("Text"));
11
var cNodeArray=new Array();
12
cNodeArray=cNode.getChildren();
13
for(j=0;j<cNodeArray.length;j++)
14
{
15
var ccNode=cNodeArray[j];
16
ccNode.setAttribute("expanded","true");
17
}
18
}
19
}
20![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
第2种递归的方法:
1
<script language="javascript">
2
3
function ExpandOneTwo()
4
{
5
//alert(arguments[0]);
6
7
var c=arguments[0];
8
var NodeArray=new Array();
9
if (c==null)
10
{
11
NodeArray=TreeView1.getChildren();
12
13
if (NodeArray!=null)
14
for(var i=0;i<NodeArray.length;i++)
15
{
16
cNode=NodeArray[i];
17
cNode.setAttribute("expanded","true");
18
if ((cNode.getParent()!=null) && (cNode.getParent().getParent()==null))
19
return;
20
ExpandOneTwo(cNode);
21
}
22
}
23
else
24
{
25
26
NodeArray=c.getChildren();
27
if (NodeArray!=null)
28
for(var i=0;i<NodeArray.length;i++)
29
{
30
cNode=NodeArray[i];
31
cNode.setAttribute("expanded","true");
32
if ((cNode.getParent()!=null) && (cNode.getParent().getParent()==null))
33
return;
34
ExpandOneTwo(cNode);
35
}
36
}
37
38
39
}
40
</script>
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/None.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)