首页 > 编程 > .NET > 正文

Asp.net DateSet绑定树型菜单(代码调试通过)

2024-07-10 13:05:48
字体:
来源:转载
供稿:网友

//第一次载入
 if (page.ispostback==false)
   {
    createdataset(); 
    bindclass(classlist, 0);
   }
关键代码
private dataset createdataset()
  {//建立数据集
   sqlconnection conn=new sqlconnection();
   conn.connectionstring=configurationsettings.appsettings

["sqlconnstring"].tostring();
   conn.open();
   string sql="select * from newsclass";
   sqldataadapter da=new sqldataadapter(sql,conn);
   ds=new dataset();
   da.fill(ds,"classtable");
   da.dispose();
   conn.dispose();
   conn.close();
   classlist.items.clear();
   classlist.items.add(new listitem("作为一级分类","0"));
   return ds;
  }
  private void bindclass(dropdownlist list,int parentid)
  {//从根节点开始递归调用显示子树 
   //classlist.datasource=ds.tables;
   //classlist.datamember="classtable";
   //classlist.databilist();

   dataview dv=new dataview();   
   listitem tmplist;
   string intid;
   dv.table=ds.tables["classtable"];
   dv.rowfilter="parentid="+parentid+"";
   foreach(datarowview drv in dv)
   {
    tmplist=new listitem();
    string depth="";
    for(int i=0;i<=int32.parse(drv["depth"].tostring());i++){
     depth+="--";
    }
    tmplist.text=depth+drv["classname"].tostring();
    tmplist.value=drv["classid"].tostring();
    list.items.add(tmplist);
    intid=drv["parentid"].tostring();
    if (drv["child"].tostring()=="1")
    {
     bindclass(classlist,int32.parse(tmplist.value));
    }    
   }
  }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表