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

[原创]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 @   ejiyuan  阅读(322)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示