[*] Hello Snoopy

.NET and Flash Blog
树形checkbox的选择(用JS实现)

ASPX页面:

<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="webapp2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm1</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        
<script language="javascript">
            
//选择所有下级节点
            function tree_onClick(tree)
            
{
                
var node = tree.getTreeNode(tree.clickedNodeIndex);
                
var isCheck = node.getAttribute("checked");
                setNodes(node,isCheck);
            }

            
function setNodes(node,isCheck)
            
{
                
var nodes = node.getChildren();
                
var node;
                
for(var i=0;i<nodes.length;i++)
                
{
                    node 
= nodes[i];
                    node.setAttribute(
"checked",isCheck);
                    setNodes(node,isCheck);
                }

            }

            
//取得所有选中节点的ID值,数祖返回
            function getNodes(tv)
            
{
                
var a = getTvNodes(tv.getChildren());
                alert(a);
            }

            
function getTvNodes(nodes)
            
{
                
var matches = [];
                
var node,isCheck;
                
for(var i=0;i<nodes.length;i++)
                
{
                    node 
= nodes[i];
                    isCheck 
= node.getAttribute("checked");
                    
if(isCheck)
                    
{
                        matches.push(node.getAttribute(
"id"));
                    }

                    matches 
= matches.concat(getTvNodes(node.getChildren()));
                }

                
return matches
            }

            function hasChild(node)
             {return node.getChildren().lenght>0;}
        
</script>
    
</HEAD>
    
<body bgColor="#cccccc">
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<P>
                    
<iewc:TreeView id="tv" runat="server" ExpandedImageUrl="images/folderOpen.gif" ImageUrl="images/folderClose.gif"
                        ShowToolTip
="False" SelectExpands="True"></iewc:TreeView><BR>
                    
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button><INPUT id="hidValues" type="hidden" name="Hidden1" runat="server">
                
</P>
            
</FONT>
            
<input type="button" value="go" onclick="getNodes(document.all.tv)"
        </form
>
    
</body>
</HTML>


CS:
//部分
this.tv.Attributes.Add("oncheck","tree_onClick(this)");

this.FillNodes(this.tv.Nodes,0);

private void FillNodes(TreeNodeCollection nodes,int ID)
        
{
            DataSet ds 
= dal.DataAdapter(CommandType.Text,
                
"SELECT * FROM Tree WHERE ParentID=" +ID.ToString(),
                
"tbl0"
                ,
null);
            
            
foreach(DataRow dataRow in ds.Tables[0].Rows)
            
{
                TreeNode node 
= new TreeNode();
                
int nodeID = (int)dataRow[0];
                
bool hasChild = this.HasChild(nodeID);
                node.ID 
= nodeID.ToString();
                node.Text 
= dataRow[1].ToString();

                node.CheckBox 
= true;
                
                
if(!hasChild)
                
{
                    node.NavigateUrl 
= "http://www.google.com";
                }

                nodes.Add(node);
                
                
if(hasChild)
                
{
                    
this.FillNodes(node.Nodes,nodeID);
                }

            }

        }

posted on 2004-08-09 16:35  HelloSnoopy  阅读(1097)  评论(0编辑  收藏  举报