[原创]TreeView的递归问题!---FAQ

问题陈述:

数据库结构
表名:Product_Class
Product_Class_id           Name            Parent            Depth
                            1                       aa               0                 1
                            2                       bb               0                 1
                            3                       aa1              1                2
                            4                       aa2              1                2
                            5                       bb1              2                2
                            6                       bb2              2                2
 表名:Product_List
Product_List_ID             Product_Class_ID           Name
                            1                        3                        aa1product
                            2                         4                        aa2product
                            3                         5                        bb1product
                            4                         6                        bb2product


     
我想用treeview控件实现如下的功能,一开始列出depth=1的所有类别名字
点一下+号列出下一级的类别,依次类推,直到点到最后一层。在最后一层中列出属于这个类别下的产品名字,并且每一个产品名字前面有一个复选框如过选中的话它的value就为它的ID值?



 

private void Page_Load(object sender, System.EventArgs e)
        
{
              TreeView1.Attributes.Add("oncheck","getV();");
           
//把2个表建立关联
            string SelectMember_Name="SELECT dbo.Product_Class.*, dbo.Product_List.Name AS Pname FROM dbo.Product_Class LEFT OUTER JOIN dbo.Product_List ON dbo.Product_Class.Product_Class_id = dbo.Product_List.Product_Class_ID";
            SqlConnection myConnection 
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString2"]);
            myConnection.Open();
            SqlCommand cmd1 
= new SqlCommand(SelectMember_Name,myConnection);
            SqlDataAdapter adp 
= new SqlDataAdapter(SelectMember_Name,myConnection);
            DataSet ds
=new DataSet();
            adp.Fill(ds);
            
this.ViewState["ds"]=ds; 
            AddTree(
0, (TreeNode)null);

        }

 

 

public void AddTree(int Parent,TreeNode pNode) 
        
{

            DataSet ds
=(DataSet) this.ViewState["ds"]; 
            DataView dvTree 
= new DataView(ds.Tables[0]);
            
//过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter =  "[Parent] = " + Parent;
            
if (dvTree.Count ==0//为最后一层节点
            {
                DataView dv 
= new DataView(ds.Tables[0]);
                dv.RowFilter
="[Product_Class_id]= "+Parent;
                
foreach(DataRowView Row in dv) 
                
{
                     TreeNode Node
=new TreeNode() ;
                       Node.CheckBox =true;
                       Node.ID=Row["Product_Class_id"].ToString();
                    Node.Text 
= Row["Pname"].ToString();
                    pNode.Nodes.Add(Node);
                }

                
return;
             }

            
foreach(DataRowView Row in dvTree) 
            
{

                TreeNode Node
=new TreeNode() ;
                
if(pNode == null
                
{    //添加根节点
                    Node.Text = Row["Name"].ToString();
                    TreeView1.Nodes.Add(Node);
                    Node.Expanded
=true;
                    AddTree(Int32.Parse(Row[
"Product_Class_ID"].ToString()), Node);    //再次递归
                }
 
                
else 
                
{  //添加当前节点的子节点

                    Node.Text 
= Row["Name"].ToString();
                    pNode.Nodes.Add(Node);
                    Node.Expanded 
= false;
                    AddTree(Int32.Parse(Row[
"Product_Class_ID"].ToString()),Node);     //再次递归

                }


            }
      
        }

 

 这个是为了看节点的ID
<script  language =javascript >
        
function getV()
        {
         
var pNode=TreeView1.getTreeNode(TreeView1.clickedNodeIndex)
         alert(pNode.getAttribute(
"ID"));
        
        }
        
        
</script>


posted @ 2005-07-06 15:05  PointNet  阅读(1196)  评论(0编辑  收藏  举报