DropDownList多级树

DropDownList多级树
Cate.xml文件内容
<? xml version="1.0" encoding="utf-8"  ?>
< Root >
  
< Content >
    
< ID > 1 </ ID >
    
< ParentID > 0 </ ParentID >
    
< CateName > 类别1 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 2 </ ID >
    
< ParentID > 1 </ ParentID >
    
< CateName > 类别2 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 3 </ ID >
    
< ParentID > 2 </ ParentID >
    
< CateName > 类别3 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 4 </ ID >
    
< ParentID > 3 </ ParentID >
    
< CateName > 类别4 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 5 </ ID >
    
< ParentID > 0 </ ParentID >
    
< CateName > 类别5 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 6 </ ID >
    
< ParentID > 0 </ ParentID >
    
< CateName > 类别6 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 7 </ ID >
    
< ParentID > 6 </ ParentID >
    
< CateName > 类别7 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 8 </ ID >
    
< ParentID > 7 </ ParentID >
    
< CateName > 类别8 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 9 </ ID >
    
< ParentID > 0 </ ParentID >
    
< CateName > 类别9 </ CateName >
  
</ Content >
  
< Content >
    
< ID > 10 </ ID >
    
< ParentID > 0 </ ParentID >
    
< CateName > 类别10 </ CateName >
  
</ Content >
</ Root >

面页 Default.aspx内容
<% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:DropDownList  ID ="ddlCate"  runat ="server" >
        
</ asp:DropDownList ></ div >
    
</ form >
</ body >
</ html >

Default.aspx.cs内容

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            BindData(
0"",0);
        }

    }

    
private void BindData(int pId,string textStr,int m)
    
{
       DataTable dt
= CateDAL.GetData(pId,Server.MapPath("Cate.xml"));
       
if (dt != null)
       
{
           
for (int i = 0; i < dt.Rows.Count; i++)
           
{

               textStr 
= "";
               
if (pId != 0)
               
{
                   m 
= m + 1;
                   
for (int j = 0; j < m; j++)
                   
{
                       textStr 
+= " ";
                   }

               }

               
else
               
{
                   m 
= 0;
               }



               
int parantID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
               ddlCate.Items.Add(
new ListItem(textStr + "" + dt.Rows[i]["CateName"].ToString(), parantID.ToString()));
               BindData(parantID, textStr, m);
           }


       }

    }

}


App_Code/CateDAL.cs文件
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/**/ /// <summary>
/// Class1 的摘要说明
/// </summary>

public   class  CateDAL
{
    
private CateDAL()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }


    
public static DataTable GetData(int parantID,string Path)
    
{
        XmlUtility xmlData 
= new XmlUtility(Path);
        DataSet ds 
= xmlData.GetData("Root/Content[ParentID=\"" + parantID + "\"]");
        
if (ds.Tables.Count != 0)
        
{
            
return ds.Tables[0];
        }

        
else
        
{
            
return null;
        }

    }

}

XmlUtility.cs文件就是介绍我的博客上有

转载于:https://www.cnblogs.com/skyblue/archive/2008/05/21/1204101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值