需要用到树性的dropdownlist
开发产品的时候,需要用到树性的dropdownlist。
首先设计好数据库,假设数据库有三个字段Id,ParentId,Name。ParentId是树性结构必须的,就是节点的父节点id,假设跟节点id都是0。下面就可以绑定了!
思路应该很简单,就是先绑定根节点,再循环绑定子节点!父节点和子节点之间为了显示出层次结构,用空格格开,不过不是space键,没用的,必须用v1的特殊字符1。
/// 绑定根节点
/// </summary>
void bindGroup()
{
DataTable dt =//获取所有节点
if(dt.Rows.Count>0)
{
foreach(DataRow row in dt.Rows)
{
if(row["ParentId"].ToString().Trim()=="0")//绑定根节点
{
this.drpItGroup.Items.Add(new ListItem(row["Name"].ToString(),row["Id"].ToString()));
this.bindDropChildItem(this.drpItGroup,dt,row["Id"].ToString(),1);
}
}
}
}
/// <summary>
/// 绑定子节点
/// </summary>
/// <param name="d"></param>
/// <param name="dt"></param>
/// <param name="id"></param>
/// <param name="length"></param>
void bindDropChildItem(DropDownList d,DataTable dt,string id,int length)
{
DataRow[] rows=dt.Select("ParentId='"+id+"'","Id DESC");//取出id子节点进行绑定
for(int i=0;i<rows.Length;i++)
{
this.drpItGroup.Items.Add(new ListItem(this.SpaceLength(length)+rows[i]["Name"].ToString(),rows[i]["Id"].ToString()));
this.bindDropChildItem(d,dt,rows[i]["Id"].ToString(),length+1);//空白数目加1
}
}
/// <summary>
/// 子节点前面的空白数
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
string SpaceLength(int i)
{
string space="";
for(int j=0;j<i;j++)
{
space+=" ";//注意这里的空白是智能abc输入法状态下的v11字符;
}
return space;
}
第一种代码:
/// <summary>
/// 绑定根节点
/// </summary>
void bindGroup()
{
DataTable dt =//获取所有节点
if(dt.Rows.Count>0)
{
foreach(DataRow row in dt.Rows)
{
if(row["ParentId"].ToString().Trim()=="0")//绑定根节点
{
this.drpItGroup.Items.Add(new ListItem(row["Name"].ToString(),row["Id"].ToString()));
this.bindDropChildItem(this.drpItGroup,dt,row["Id"].ToString(),1);
}
}
}
}
/**//// <summary>
/// 绑定子节点
/// </summary>
/// <param name="d"></param>
/// <param name="dt"></param>
/// <param name="id"></param>
/// <param name="length"></param>
void bindDropChildItem(DropDownList d,DataTable dt,string id,int length)
{
DataRow[] rows=dt.Select("ParentId='"+id+"'","Id DESC");//取出id子节点进行绑定
for(int i=0;i<rows.Length;i++)
{
this.drpItGroup.Items.Add(new ListItem(this.SpaceLength(length)+rows[i]["Name"].ToString(),rows[i]["Id"].ToString()));
this.bindDropChildItem(d,dt,rows[i]["Id"].ToString(),length+1);//空白数目加1
}
}
/**//// <summary>
/// 子节点前面的空白数
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
string SpaceLength(int i)
{
string space="";
for(int j=0;j<i;j++)
{
space+=" ";//注意这里的空白是智能abc输入法状态下的v11字符;
}
return space;
}
这样就大功告成了,绑定一下就可以看到效果了!还可以在客户端进行添加节点,方法相似!
第2种:
/// 模拟分级显示的下拉框 的摘要说明。
/// </summary>
public class 模拟分级显示的下拉框 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
DataTable myData = this.GetDataFromDB();
CreateLevelDropDown(this.DropDownList1,myData);
}
}
/// <summary>
/// 模拟一些调试数据,实际应用改为从数据库读取
/// </summary>
/// <returns></returns>
private DataTable GetDataFromDB()
{
DataTable dt = new DataTable();
dt.Columns.Add("classID",typeof(string));
dt.Columns.Add("PID",typeof(string));
dt.Columns.Add("className",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.Rows.Add(new object[]{10,1,"二级ZZZZ"});
dt.AcceptChanges();
return dt;
}
/// <summary>
/// 创建分级下拉框
/// </summary>
private void CreateLevelDropDown(DropDownList ddlst,DataTable dt)
{
System.Collections.ArrayList allItems = new ArrayList();
DataRow[] rows = dt.Select("[pID]='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["className"].ToString(),parentRow["classID"].ToString());
items.Add(newItem);
parentRow.Delete();
DataRow[] rows = dt.Select("[pID]='"+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("┗",".")+"┗");
// foreach(DataRow row in rows)
// CreateLevelDropDownAssistant(dt,ref items,row,curHeader.Replace("┣","┃")+"┣");
}
/**//// <summary>
/// 模拟分级显示的下拉框 的摘要说明。
/// </summary>
public class 模拟分级显示的下拉框 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
DataTable myData = this.GetDataFromDB();
CreateLevelDropDown(this.DropDownList1,myData);
}
}
/**//// <summary>
/// 模拟一些调试数据,实际应用改为从数据库读取
/// </summary>
/// <returns></returns>
private DataTable GetDataFromDB()
{
DataTable dt = new DataTable();
dt.Columns.Add("classID",typeof(string));
dt.Columns.Add("PID",typeof(string));
dt.Columns.Add("className",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.Rows.Add(new object[]{10,1,"二级ZZZZ"});
dt.AcceptChanges();
return dt;
}
/**//// <summary>
/// 创建分级下拉框
/// </summary>
private void CreateLevelDropDown(DropDownList ddlst,DataTable dt)
{
System.Collections.ArrayList allItems = new ArrayList();
DataRow[] rows = dt.Select("[pID]='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["className"].ToString(),parentRow["classID"].ToString());
items.Add(newItem);
parentRow.Delete();
DataRow[] rows = dt.Select("[pID]='"+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("┗",".")+"┗");
// foreach(DataRow row in rows)
// CreateLevelDropDownAssistant(dt,ref items,row,curHeader.Replace("┣","┃")+"┣");
}
第3种:
public void AddDropDownList (System.Web.UI.WebControls.DropDownList droplist, int ParentID, ListItem plist) //DataSet dsFlowType = myData.GetFlowTypeDS(); //过滤ParentId,得到当前的所有子节点 string blank = ""; } //DataSet dsFlowType = myData.GetFlowTypeDS(); |