treeview递归

来源:互联网 发布:51单片机c语言教程全集 编辑:程序博客网 时间:2024/05/21 12:51
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewCheck.aspx.cs" Inherits="TEST_TreeViewCheck" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title> 


    <script language="javascript"> 
        function CheckSubNodes() {
            var obj = window.event.srcElement;
            var TreeNodeFound = false;
            var CheckedState;
            if (obj.tagName == "INPUT" && obj.type == "checkbox") {
                var TreeNode = obj;
                CheckedState = obj.checked;
                do {
                    obj = obj.parentNode;


                }
                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 (var 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>
     
</head>
<body>
    <form id="form1" runat="server">
         <table width=100% height=100%>
            <tr height=10>
                <td><input id="chkall" type="checkbox"   />全选/取消</td>
                <td><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td>
            </tr>
            <tr valign=top>
                <td><asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" onclick="CheckSubNodes();" ShowLines="True" >
                    <Nodes>
                        <asp:TreeNode Text="1" Value="New Node"></asp:TreeNode>
                        <asp:TreeNode ShowCheckBox="True" Text="2" Value="New Node">
                            <asp:TreeNode ShowCheckBox="True" Text="21" Value="New Node">
                                <asp:TreeNode SelectAction="None" ShowCheckBox="True" Text="New Node" Value="New Node">
                                    <asp:TreeNode PopulateOnDemand="True" SelectAction="None" Selected="True" ShowCheckBox="True" Text="New Node" Value="New Node"></asp:TreeNode>
                                </asp:TreeNode>
                            </asp:TreeNode>
                            <asp:TreeNode Text="22" Value="22">
                                <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                            </asp:TreeNode>
                            <asp:TreeNode Text="23" Value="23"></asp:TreeNode>
                        </asp:TreeNode>
                        <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                        <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                        <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                        <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                        <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                    </Nodes>
                    </asp:TreeView></td>
                <td><iframe id=fMain style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none"
                            src="" frameBorder="0" width="100%" scrolling="yes" height="100%"></iframe></td>
            </tr>
            <tr height=10>
                <td></td>
            </tr>
        </table>


    </form>
</body>
</html>
原创粉丝点击