DropdownList下拉式菜单,无限分类的
分类A
├分类A1
分类B
├分类B1
│├分类B2
高手帮忙啊!!
数据库设计(ACCESS或SQL都可以啊)
classid自动编号
pid   数字      
classname文本  分类名称后台有个添加页面,
前台DropdownList下拉式菜单,无限分类的望CSDN的大虾帮忙啊!!!!那位大哥能不能给个详细的代码啊。。
求求CSDN的大哥们拉
急用啊。。毕业论文用啊!!!!
我上次的毕业论文被打下来了啊!!!

解决方案 »

  1.   

    兄弟,看下我的问题吧,我到现在还是一知半解
    http://community.csdn.net/Expert/topic/5158/5158852.xml?temp=.530636
    我的帖子
    前台显示,好象这样的吧!
    /// <summary>
    /// 简单类型信息
    /// </summary>
    public class SimpleTypeInfo
    {
    private int simpleTypeId;
    private string typeName=string.Empty;
    private int parentId;
    private SimpleTypeInfo parentInfo=null;
    private SimpleTypeInfoCollection childs=null;public int SimpleTypeID
    {
    get{return simpleTypeId;}
    set{simpleTypeId=value;}
    }public string TypeName
    {
    get{return typeName;}
    set{typeName=value;}
    }public int ParentID
    {
    get{return parentId;}
    set{parentId=value;}
    }public SimpleTypeInfo ParentInfo
    {
    get{return parentInfo;}
    set{parentInfo=value;}
    }public SimpleTypeInfoCollection Childs
    {
    get{return childs;}
    set{childs=value;}
    }public SimpleTypeInfo(){}public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
    {
    this.simpleTypeId=simpleTypeId;
    this.typeName=typeName;
    this.parentId=parentId;
    this.parentInfo=parentInfo;
    this.childs=childs;
    }
    }
    ---------------------------------------------------------------------------
    /// <summary>
    /// 简单类型信息集合
    /// </summary>
    public class SimpleTypeInfoCollection:CollectionBase
    {
    public SimpleTypeInfoCollection(){}public SimpleTypeInfo this[int index]
    {
    get{return (SimpleTypeInfo)List[index];}
    set{List[index]=value;}
    }public int Add(SimpleTypeInfo value)
    {
    return List.Add(value);
    }public int IndexOf(SimpleTypeInfo value)
    {
    return List.IndexOf(value);
    }public void Insert(int index,SimpleTypeInfo value)
    {
    List.Insert(index,value);
    }public void Remove(SimpleTypeInfo value)
    {
    List.Remove(value);
    }public bool Contains(SimpleTypeInfo value)
    {
    return List.Contains(value);
    }}
    --------------------------------------------------------------
    /// <summary>
    /// 简单类型接口
    /// </summary>
    public interface ISimpleType
    {
    SimpleTypeInfoCollection getTreeTypes();
    }
    ----------------------------------------------------------------
    /// <summary>
    /// SqlServer实现类
    /// </summary>
    public class SqlSimpleType:ISimpleType
    {
    protected string connectionString=@"database=InputDB;server=surely\mysql;user id=sa;password=123";
    public SqlSimpleType(){}#region ISimpleType 成员/// <summary>
    /// 返回树对象
    /// </summary>
    /// <returns></returns>
    public SimpleTypeInfoCollection getTreeTypes()
    {
    DataTable dt=this.getSimpleTypes(connectionString);
    if(dt==null)
    return null;
    IDictionary all=new Hashtable();
    for(int n=0;n<dt.Rows.Count;n++)
    {
    SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
    if(info!=null)
    all.Add(info.SimpleTypeID,info);
    }SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
    return types;
    }/// <summary>
    /// 根据Hash创建树结构对象
    /// </summary>
    /// <param name="all"></param>
    /// <returns></returns>
    protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
    {
    if(all==null||all.Count<=0)
    return null;
    IDictionaryEnumerator de=all.GetEnumerator();
    SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
    while(de.MoveNext())
    {
    SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
    //只添加根
    if(info.ParentID<=0)
    collection.Add(info);
    if(all.Contains(info.ParentID))
    {
    SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
    if(parentInfo.Childs==null)
    parentInfo.Childs=new SimpleTypeInfoCollection();
    parentInfo.Childs.Add(info);
    info.ParentInfo=parentInfo;
    }
    }
    return collection;
    }/// <summary>
    /// 返回简单类型表中的所有数据
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    protected DataTable getSimpleTypes(string url)
    {
    string sql="Select * From SimpleType";
    SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
    SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataTable dt=new DataTable();
    da.Fill(dt);
    return dt;
    }/// <summary>
    /// 转换简单类型信息
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    protected SimpleTypeInfo ConvertType(DataRow dr)
    {
    if(dr==null)
    return null;
    SimpleTypeInfo info=new SimpleTypeInfo();
    info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
    info.TypeName=Convert.ToString(dr["TypeName"]);
    info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
    return info;
    }#endregion
    }
    -----------------------------------------调用方法----------------------
    ISimpleType s=new SqlSimpleType();
    SimpleTypeInfoCollection collection=s.getTreeTypes();
    collection则是树结构对象
    如果数据库数据量很大的话,不要使用本类,时间关系就不写针对大数据量加载方式了.数据库结构
    SimpleTypeID(主键),TypeName,ParentID
    1,一级分类,null
    2,二级分类1,1
    3,二级分类2,1
    4,三级分类2,3
    5,二级分类3,1
    6,三级分类3,5
    7,四级分类3,6先建个表,一会把程序中的连接字符串改改lxwin01(阿幸) 的,可惜我还是没成功......望你能够懂...但是....下面的这个应该可以符合你吧
    protected System.Web.UI.WebControls.DropDownList DropDownList1;private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    DataTable myData = this.CreateTestData();CreateLevelDropDown(this.DropDownList1,myData);
    }
    }/// <summary>
    /// 模拟一些测试数据,应用时改为从数据库读取
    /// </summary>
    /// <returns></returns>
    private DataTable CreateTestData()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("ID",typeof(string));
    dt.Columns.Add("ParentID",typeof(string));
    dt.Columns.Add("TypeName",typeof(string));
    dt.Rows.Add(new object[]{1,0,"一级分类"});//假定顶级节点只有一个,并且ParentID为0
    dt.Rows.Add(new object[]{2,1,"二级分类1"});
    dt.Rows.Add(new object[]{3,1,"二级分类2"});
    dt.Rows.Add(new object[]{4,3,"三级分类2"});
    dt.Rows.Add(new object[]{5,1,"二级分类3"});
    dt.Rows.Add(new object[]{6,5,"三级分类3"});
    dt.Rows.Add(new object[]{7,6,"四级分类3"});
    dt.Rows.Add(new object[]{8,3,"三级AAAA"});
    dt.Rows.Add(new object[]{9,3,"三级BBBB"});dt.AcceptChanges();
    return dt;
    }/// <summary>
    /// 创建分级下拉框
    /// </summary>
    private void CreateLevelDropDown(DropDownList ddlst,DataTable dt)
    {
    System.Collections.ArrayList allItems = new ArrayList();
    DataRow[] rows = dt.Select("[ParentID]='0'");
    foreach(DataRow row in rows)
    CreateLevelDropDownAssistant(dt,ref allItems,row,string.Empty);ListItem[] items = new ListItem[allItems.Count];
    allItems.CopyTo(items);
    ddlst.Items.AddRange(items);
    }
    private void CreateLevelDropDownAssistant(DataTable dt,ref ArrayList items,DataRow parentRow,string curHeader)
    {
    ListItem newItem = new ListItem(curHeader+parentRow["TypeName"].ToString(),parentRow["ID"].ToString());
    items.Add(newItem);
    parentRow.Delete();DataRow[] rows = dt.Select("[ParentID]='"+newItem.Value+"'");
    for(int i=0;i<rows.Length-1;i++)
    CreateLevelDropDownAssistant(dt,ref items,rows[i],curHeader.Replace("┣","┃").Replace("┗"," ")+"┣");if(rows.Length>0)
    CreateLevelDropDownAssistant(dt,ref items,rows[rows.Length-1],curHeader.Replace("┣","┃").Replace("┗",".")+"┗");
    }如果你搞懂了,记得把原代码发给我一份
    我的QQ号码是:[email protected]
    严重谢谢,希望对你有用,
    我前些天问的问题,
    到现在都没解决,
    人太笨了没办法
      

  2.   

    tanglang86() 
    能不能发份详细原代码给我,
    发的太混乱了
    我看不懂