日期:2011-10-04 浏览次数:20544 次
//第一次载入
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));
}
}
}