//第一次载入
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));
}
}
}
新闻热点
疑难解答
图片精选