VS2005 TreeView 的 CheckBox 被点击时的引发页面回发事件 (转发)
解决原有TreeView 的 CheckBox 被点击时不能引发服务器端事件
实现当TreeView中父节点的CheckBox的点选状态改变时
回发服务器端
触发TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)事件
从而通过代码来控制子节点的CheckBox的点选状态 一同改变
主要步骤及代码示例如下:
1.TreeView绑定onclick事件
通过本事件 引发回发
2.在TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)事件
编写相关代码 控制子节点的CheckBox点选状态
-------------------- HTML 页面部分 -----------------------
1<html>
2<head runat="server">
3 <title>VS2005 TreeView CheckBox</title>
4 <script type="text/javascript">
5 // 点击复选框时触发事件
6 function postBackByObject()
7 {
8 var o = window.event.srcElement;
9 if (o.tagName == "INPUT" && o.type == "checkbox")
10 {
11 __doPostBack("","");
12 }
13 }
14</script>
15</head>
16<body>
17 <form id="form1" runat="server">
18 <div>
19 <asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
20 <SelectedNodeStyle BackColor="SlateGray" />
21 </asp:TreeView>
22 </div>
23 </form>
24</body>
25</html>
2<head runat="server">
3 <title>VS2005 TreeView CheckBox</title>
4 <script type="text/javascript">
5 // 点击复选框时触发事件
6 function postBackByObject()
7 {
8 var o = window.event.srcElement;
9 if (o.tagName == "INPUT" && o.type == "checkbox")
10 {
11 __doPostBack("","");
12 }
13 }
14</script>
15</head>
16<body>
17 <form id="form1" runat="server">
18 <div>
19 <asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
20 <SelectedNodeStyle BackColor="SlateGray" />
21 </asp:TreeView>
22 </div>
23 </form>
24</body>
25</html>
--------------------- cs页面代码 --------------------
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 //构建TreeView
6 TreeNode Node00 = new TreeNode("00", "00");
7
8 TreeNode Node01 = new TreeNode("01", "01");
9 TreeNode Node0101 = new TreeNode("0101", "0101");
10 TreeNode Node0102 = new TreeNode("0102", "0102");
11 TreeNode Node02 = new TreeNode("02", "02");
12
13 Node01.ChildNodes.Add(Node0101);
14 Node01.ChildNodes.Add(Node0102);
15
16 Node00.ChildNodes.Add(Node01);
17 Node00.ChildNodes.Add(Node02);
18
19 TreeView1.Nodes.Add(Node00);
20
21 TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox
22
23 TreeView1.ExpandDepth = 0;
24
25 }
26 //绑定事件
27 TreeView1.Attributes.Add("onclick", "postBackByObject()");
28
29 }
30
31 //
32 protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
33 {
34 SetChildChecked(e.Node);
35 }
36
37 //
38 private void SetChildChecked(TreeNode parentNode)
39 {
40 foreach (TreeNode node in parentNode.ChildNodes)
41 {
42 node.Checked = parentNode.Checked;
43
44 if (node.ChildNodes.Count > 0)
45 {
46 SetChildChecked(node);
47 }
48 }
49 }
50
51
2 {
3 if (!IsPostBack)
4 {
5 //构建TreeView
6 TreeNode Node00 = new TreeNode("00", "00");
7
8 TreeNode Node01 = new TreeNode("01", "01");
9 TreeNode Node0101 = new TreeNode("0101", "0101");
10 TreeNode Node0102 = new TreeNode("0102", "0102");
11 TreeNode Node02 = new TreeNode("02", "02");
12
13 Node01.ChildNodes.Add(Node0101);
14 Node01.ChildNodes.Add(Node0102);
15
16 Node00.ChildNodes.Add(Node01);
17 Node00.ChildNodes.Add(Node02);
18
19 TreeView1.Nodes.Add(Node00);
20
21 TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox
22
23 TreeView1.ExpandDepth = 0;
24
25 }
26 //绑定事件
27 TreeView1.Attributes.Add("onclick", "postBackByObject()");
28
29 }
30
31 //
32 protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
33 {
34 SetChildChecked(e.Node);
35 }
36
37 //
38 private void SetChildChecked(TreeNode parentNode)
39 {
40 foreach (TreeNode node in parentNode.ChildNodes)
41 {
42 node.Checked = parentNode.Checked;
43
44 if (node.ChildNodes.Count > 0)
45 {
46 SetChildChecked(node);
47 }
48 }
49 }
50
51