另种思路:图片转换 变通模拟实现VS2005下TreeView的ChcekBox点击
为了实现VS2005下TreeView的ChcekBox 点击
根据张Sir的指导(感谢他的无私和指导)
提供如下解决思路:
用两副图片checkboxUncheck.gif 和 checkboxClicked.gif
轮流作为每个TreeNode的ImageUrl
每次点击TreeNode时的回发 都切换图片的显示样式
同时将TreeNode的Value值保存到隐藏的文本框
以便可以得到有哪些节点是Checked
------------------
部分示例代码如下
------------------
+++++++++++++
treeMenu 绑定
+++++++++++++
1 treeMenu 绑定#region treeMenu 绑定
2 private void treeMenuBind()
3 {
4 //构建TreeView
5
6 TreeNode Node00 = new TreeNode("00", "00");
7 Node00..ImageUrl = "../Images/checkboxUnClick.gif";
8
9 TreeNode Node01 = new TreeNode("01", "01");
10 Node01.ImageUrl = "../Images/checkboxUnClick.gif";
11
12 TreeNode Node0101 = new TreeNode("0101", "0101");
13 Node0101.ImageUrl = "../Images/checkboxUnClick.gif";
14
15 TreeNode Node0102 = new TreeNode("0102", "0102");
16 Node0102.ImageUrl = "../Images/checkboxUnClick.gif";
17
18 TreeNode Node02 = new TreeNode("02", "02");
19 Node02.ImageUrl = "../Images/checkboxUnClick.gif";
20
21 Node01.ChildNodes.Add(Node0101);
22 Node01.ChildNodes.Add(Node0102);
23
24 Node00.ChildNodes.Add(Node01);
25 Node00.ChildNodes.Add(Node02);
26
27 TreeView1.Nodes.Add(Node00);
28
29 //TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox
30
31 TreeView1.ExpandDepth = 2;
32
33 }
34 #endregion
35
36
2 private void treeMenuBind()
3 {
4 //构建TreeView
5
6 TreeNode Node00 = new TreeNode("00", "00");
7 Node00..ImageUrl = "../Images/checkboxUnClick.gif";
8
9 TreeNode Node01 = new TreeNode("01", "01");
10 Node01.ImageUrl = "../Images/checkboxUnClick.gif";
11
12 TreeNode Node0101 = new TreeNode("0101", "0101");
13 Node0101.ImageUrl = "../Images/checkboxUnClick.gif";
14
15 TreeNode Node0102 = new TreeNode("0102", "0102");
16 Node0102.ImageUrl = "../Images/checkboxUnClick.gif";
17
18 TreeNode Node02 = new TreeNode("02", "02");
19 Node02.ImageUrl = "../Images/checkboxUnClick.gif";
20
21 Node01.ChildNodes.Add(Node0101);
22 Node01.ChildNodes.Add(Node0102);
23
24 Node00.ChildNodes.Add(Node01);
25 Node00.ChildNodes.Add(Node02);
26
27 TreeView1.Nodes.Add(Node00);
28
29 //TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox
30
31 TreeView1.ExpandDepth = 2;
32
33 }
34 #endregion
35
36
+++++++++++++
TreeView 点击 事件
+++++++++++++++++
1TreeView 点击 事件#region TreeView 点击 事件
2 protected void treeMenu_SelectedNodeChanged(object sender, EventArgs e)
3 {
4 try
5 {
6 //当前图片路径及名称
7
8 string imgName = this.treeMenu.SelectedNode.ImageUrl.ToString().Trim();
9
10 if (imgName.Equals("../Images/checkboxUnClick.gif"))//未选中->选中
11 {
12 当前节点 及 子节点 和 父节点#region 当前节点 及 子节点 和 父节点
13
14 //当前节点
15 this.treeMenu.SelectedNode.ImageUrl = "../Images/checkboxClicked.gif";
16 //判断当前是否已经存有值 以防重复加入
17 if(!this.hidtxt_LevelID.Text.Trim().Contains(this.treeMenu.SelectedNode.Value.Trim() + ", "))
18 this.hidtxt_LevelID.Text += " " + this.treeMenu.SelectedNode.Value + ", ";
19 //子节点
20
21 setChildNode(this.treeMenu.SelectedNode, "1");
22 //父节点
23
24 setParentNode(this.treeMenu.SelectedNode);
25 #endregion
26 }
27 else//选中->未选中
28 {
29 当前节点 及 子节点#region 当前节点 及 子节点
30
31 //当前节点 及 子节点
32
33 this.treeMenu.SelectedNode.ImageUrl = "../Images/checkboxUnClick.gif";
34 this.hidtxt_LevelID.Text=this.hidtxt_LevelID.Text.Replace(" "+this.treeMenu.SelectedNode.Value + ",", " ");
35
36 //子节点
37
38 setChildNode(this.treeMenu.SelectedNode,"0");
39 #endregion
40 }
41
42 treeMenu.SelectedNode.Selected = false;
43 }
44 catch (Exception ex)
45 {
46 ex.ToString();
47 }
48 }
49 #endregion
50
51
2 protected void treeMenu_SelectedNodeChanged(object sender, EventArgs e)
3 {
4 try
5 {
6 //当前图片路径及名称
7
8 string imgName = this.treeMenu.SelectedNode.ImageUrl.ToString().Trim();
9
10 if (imgName.Equals("../Images/checkboxUnClick.gif"))//未选中->选中
11 {
12 当前节点 及 子节点 和 父节点#region 当前节点 及 子节点 和 父节点
13
14 //当前节点
15 this.treeMenu.SelectedNode.ImageUrl = "../Images/checkboxClicked.gif";
16 //判断当前是否已经存有值 以防重复加入
17 if(!this.hidtxt_LevelID.Text.Trim().Contains(this.treeMenu.SelectedNode.Value.Trim() + ", "))
18 this.hidtxt_LevelID.Text += " " + this.treeMenu.SelectedNode.Value + ", ";
19 //子节点
20
21 setChildNode(this.treeMenu.SelectedNode, "1");
22 //父节点
23
24 setParentNode(this.treeMenu.SelectedNode);
25 #endregion
26 }
27 else//选中->未选中
28 {
29 当前节点 及 子节点#region 当前节点 及 子节点
30
31 //当前节点 及 子节点
32
33 this.treeMenu.SelectedNode.ImageUrl = "../Images/checkboxUnClick.gif";
34 this.hidtxt_LevelID.Text=this.hidtxt_LevelID.Text.Replace(" "+this.treeMenu.SelectedNode.Value + ",", " ");
35
36 //子节点
37
38 setChildNode(this.treeMenu.SelectedNode,"0");
39 #endregion
40 }
41
42 treeMenu.SelectedNode.Selected = false;
43 }
44 catch (Exception ex)
45 {
46 ex.ToString();
47 }
48 }
49 #endregion
50
51
++++++++++++
设置子节点的图片和当前节点一致
++++++++++++++++++++++++++++++
1 设置子节点的图片和当前节点一致#region 设置子节点的图片和当前节点一致
2
3 //设置子节点的图片和当前节点一致
4
5 private void setChildNode(TreeNode currNode ,string imgType)
6 {
7 if (imgType.Equals("1"))//未选中->选中
8 {
9 foreach (TreeNode node in currNode.ChildNodes)
10 {
11 if (node.ImageUrl.Equals("../Images/checkboxUnClick.gif"))
12 {
13 node.ImageUrl = "../Images/checkboxClicked.gif";
14 if (!this.hidtxt_LevelID.Text.Trim().Contains(" " + node.Value.Trim() + ", "))
15 this.hidtxt_LevelID.Text += " " + node.Value + ", ";
16 }
17 //递归
18 if (node.ChildNodes.Count > 0)
19 {
20 setChildNode(node,"1");
21 }
22 }
23 }
24 else //选中->未选中
25 {
26 foreach (TreeNode node in currNode.ChildNodes)
27 {
28 if (node.ImageUrl.Equals("../Images/checkboxClicked.gif"))
29 {
30 node.ImageUrl = "../Images/checkboxUnClick.gif";
31
32 this.hidtxt_LevelID.Text = this.hidtxt_LevelID.Text.Replace(" " + node.Value + ",", " ");
33 }
34 //递归
35 if (node.ChildNodes.Count > 0)
36 {
37 setChildNode(node,"0");
38 }
39 }
40 }
41 }
42 #endregion
43
44
2
3 //设置子节点的图片和当前节点一致
4
5 private void setChildNode(TreeNode currNode ,string imgType)
6 {
7 if (imgType.Equals("1"))//未选中->选中
8 {
9 foreach (TreeNode node in currNode.ChildNodes)
10 {
11 if (node.ImageUrl.Equals("../Images/checkboxUnClick.gif"))
12 {
13 node.ImageUrl = "../Images/checkboxClicked.gif";
14 if (!this.hidtxt_LevelID.Text.Trim().Contains(" " + node.Value.Trim() + ", "))
15 this.hidtxt_LevelID.Text += " " + node.Value + ", ";
16 }
17 //递归
18 if (node.ChildNodes.Count > 0)
19 {
20 setChildNode(node,"1");
21 }
22 }
23 }
24 else //选中->未选中
25 {
26 foreach (TreeNode node in currNode.ChildNodes)
27 {
28 if (node.ImageUrl.Equals("../Images/checkboxClicked.gif"))
29 {
30 node.ImageUrl = "../Images/checkboxUnClick.gif";
31
32 this.hidtxt_LevelID.Text = this.hidtxt_LevelID.Text.Replace(" " + node.Value + ",", " ");
33 }
34 //递归
35 if (node.ChildNodes.Count > 0)
36 {
37 setChildNode(node,"0");
38 }
39 }
40 }
41 }
42 #endregion
43
44
++++++++++++++
设置父节点为选中
++++++++++++++++++
1设置父节点为选中#region 设置父节点为选中
2 //设置父节点为选中
3 private void setParentNode(TreeNode currNode)
4 {
5 if (currNode.Parent != null)
6 {
7 //只有当前父节点未选中时 才须进行图片置换 以及 保存LevelID
8 if (currNode.Parent.ImageUrl.Equals("../Images/checkboxUnClick.gif"))
9 {
10 currNode.Parent.ImageUrl = "../Images/checkboxClicked.gif";
11 if (!this.hidtxt_LevelID.Text.Trim().Contains(" " + currNode.Parent.Value.Trim() + ", "))
12 this.hidtxt_LevelID.Text += " " + currNode.Parent.Value + ", ";
13 }
14 //递归上寻
15 if (currNode.Parent.Parent != null)
16 {
17 setParentNode(currNode.Parent);
18 }
19 }
20 }
21 #endregion
22
2 //设置父节点为选中
3 private void setParentNode(TreeNode currNode)
4 {
5 if (currNode.Parent != null)
6 {
7 //只有当前父节点未选中时 才须进行图片置换 以及 保存LevelID
8 if (currNode.Parent.ImageUrl.Equals("../Images/checkboxUnClick.gif"))
9 {
10 currNode.Parent.ImageUrl = "../Images/checkboxClicked.gif";
11 if (!this.hidtxt_LevelID.Text.Trim().Contains(" " + currNode.Parent.Value.Trim() + ", "))
12 this.hidtxt_LevelID.Text += " " + currNode.Parent.Value + ", ";
13 }
14 //递归上寻
15 if (currNode.Parent.Parent != null)
16 {
17 setParentNode(currNode.Parent);
18 }
19 }
20 }
21 #endregion
22
posted on 2007-01-08 11:04 freeliver54 阅读(890) 评论(0) 编辑 收藏 举报