soveran

My Links

Blog Stats

如何取出treeview中checkbox的值

这是aspx的内容:
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %> 



  

<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@ Page language="c#"  SmartNavigation="true" Codebehind="test1.aspx.cs" AutoEventWireup="false" Inherits="tkSystem.test1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>test1</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 initchecknode()
{
 var node
=TreeView1.getTreeNode("1");
 node.setAttribute(
"Checked","true");
 setcheck(node,
"true"); 
 FindCheckedFromNode(TreeView1);
}

//oncheck事件
function tree_oncheck(tree)
{
 var node
=tree.getTreeNode(tree.clickedNodeIndex);
 var Pchecked
=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
 setcheck(node,Pchecked);
 document.all.
checked.value="";
 document.all.
unchecked.value="";
 FindCheckedFromNode(TreeView1);
}


//设置子节点选中
function setcheck(node,Pc)
{
 var i;
 var ChildNode
=new Array();
 ChildNode
=node.getChildren();
 
 
if(parseInt(ChildNode.length)==0)
  
return;
 
else
 
{
  
for(i=0;i<ChildNode.length;i++)
  
{
   var cNode;
   cNode
=ChildNode[i];
   
if(parseInt(cNode.getChildren().length)!=0)
    setcheck(cNode,Pc);
   cNode.setAttribute(
"Checked",Pc);
  }

 }

}

//获取所有节点状态
function FindCheckedFromNode(node) {
 var i 
= 0;
 var nodes 
= new Array();
 nodes 
= node.getChildren();
 
 
for (i = 0; i < nodes.length; i++{
  var cNode;
  cNode
=nodes[i];
  
if (cNode.getAttribute("Checked"))
   AddChecked(cNode);
  
else
      AddUnChecked(cNode);
  
  
if (parseInt(cNode.getChildren().length) != 0 ) {
   FindCheckedFromNode(cNode);
  }

 }

}

//添加选中节点
function AddChecked(node) {
 
document.all.checked.value += node.getAttribute("NodeData");
 
document.all.checked.value += ',';
 }

//添加未选中节点
function AddUnChecked(node) {
 document.all.
unchecked.value += node.getAttribute("NodeData");
 document.all.
unchecked.value += ',';
}


        
</script>
        
<script language="javascript">
  
        
</script>
    
</HEAD>
    
<body onload="initchecknode()" MS_POSITIONING="FlowLayout">
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<iewc:treeview id="TreeView1" runat="server">
                    
<iewc:TreeNode NodeData="1" CheckBox="True" Text="第一章" Expanded="True"></iewc:TreeNode>
    
<iewc:TreeNode NodeData="1" CheckBox="True" Text="第二章" Expanded="True"></iewc:TreeNode>
            
</iewc:treeview><br>
                
<P>
                    
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
                        
<TR>
                            
<TD><asp:label id="Label1" runat="server">checked</asp:label></TD>
                            
<!--    <TD><INPUT id="checked" type="text" size="32"></TD>-->
                        
</TR>
                        
<TR>
                            
<TD><asp:label id="Label2" runat="server">unchecked</asp:label></TD>
                            
<TD><INPUT id="unchecked" type="text" size="32"></TD>
                        
</TR>
                    
</TABLE>
                    
<!--<input type="button" value="set check" onclick="set_check()">-->
                    
<br>
                    
&nbsp;
                    
<asp:Button id="Button1" runat="server" Text="submit"></asp:Button>
                    
            </FONT>
        
</form>
        
</P>
    
</body>
</HTML>

 

<%@ Page language="c#" SmartNavigation="true" Codebehind="test1.aspx.cs" AutoEventWireup="false" Inherits="tkSystem.test1" %>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.Windows.Forms;


namespace tkSystem
{
    
/// <summary>
    
/// test1 的摘要说明。
    
/// </summary>

    public class test1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.HtmlControls.HtmlInputHidden HdnSelectedValues;
    
protected System.Web.UI.WebControls.Button Button1;
        
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!IsPostBack)
            
{
                TreeView1.Attributes.Add(
"oncheck","tree_oncheck(this)");
                            }

            
            
            
        }

        
        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
string zhangjie="";
            
if(TreeView1.Nodes[0].Nodes[0].Checked)
                zhangjie
+=TreeView1.Nodes[0].Nodes[0].Text.ToString()+",";
            
if(TreeView1.Nodes[0].Nodes[1].Checked)
                zhangjie
+=TreeView1.Nodes[0].Nodes[1].Text.ToString()+",";
            
if(TreeView1.Nodes[0].Nodes[2].Checked) 
                zhangjie
+=TreeView1.Nodes[0].Nodes[2].Text.ToString()+",";
            
if(TreeView1.Nodes[0].Nodes[3].Checked)
                zhangjie
+=TreeView1.Nodes[0].Nodes[3].Text.ToString()+",";
            
if(TreeView1.Nodes[0].Nodes[4].Checked)
                zhangjie
+=TreeView1.Nodes[0].Nodes[4].Text.ToString()+",";
            
if(TreeView1.Nodes[0].Nodes[5].Checked)
                zhangjie
+=TreeView1.Nodes[0].Nodes[5].Text.ToString();    
            
if(zhagnjie!="")
            Response.Write(zhangjie);
            
        }


    



    }

}

 

 


 

posted on 2006-06-05 10:56  soveran  阅读(...)  评论(...编辑  收藏