[原创]如何控制TreeView在打开的时候只展开两层?
树的Html代码如下:
别忘了在窗体的onload事件中调用ExpandOneTwo()函数
<body MS_POSITIONING="GridLayout" onload="ExpandOneTwo();">

第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>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

别忘了在窗体的onload事件中调用ExpandOneTwo()函数


第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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

第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>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
