asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树
本文实例讲述了asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树。分享给大家供大家参考,具体如下:
#regionDropDownList无限递归显示层次关系 ///<summary> ///创建无限分级下拉列表框 ///</summary> ///<paramname="ddlst">下拉控件</param> ///<paramname="dt">源DataTable</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> publicstaticvoidCreateLevelDropDown(DropDownListddlst,DataTabledt,stringtext,stringvalue,stringparentid) { ArrayListallItems=newArrayList(); DataRow[]rows=dt.Select(parentid+"=0"); foreach(DataRowrowinrows) CreateLevelDropDownAssistant(dt,refallItems,row,string.Empty,text,value,parentid); ListItem[]items=newListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } ///<summary> ///递归绑定子节点 ///</summary> ///<paramname="dt">源DataTable</param> ///<paramname="items">数组</param> ///<paramname="parentRow">当前节点</param> ///<paramname="curHeader">前缀</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> privatestaticvoidCreateLevelDropDownAssistant(DataTabledt,refArrayListitems,DataRowparentRow,stringcurHeader,stringtext,stringvalue,stringparentid) { ListItemnewItem=newListItem(curHeader+Until.CutString(parentRow[text].ToString(),18,true),parentRow[value].ToString()); items.Add(newItem); DataRow[]rows=dt.Select(parentid+"="+newItem.Value); for(inti=0;i<rows.Length-1;i++) CreateLevelDropDownAssistant(dt,refitems,rows[i],curHeader.Replace("┣","┃").Replace("┗","┣")+"┣",text,value,parentid); if(rows.Length>0) CreateLevelDropDownAssistant(dt,refitems,rows[rows.Length-1],curHeader.Replace("┣","┃").Replace("┗","┣")+"┗",text,value,parentid); } #endregion #regionTreeView无限递归显示层次关系目录树 ///<summary> ///创建无限分级目录树TreeView ///</summary> ///<paramname="treeview">TreeView空间</param> ///<paramname="dt">数据源DataTable</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> publicstaticvoidCreateLevelTreeView(TreeViewtreeview,DataTabledt,stringtext,stringvalue,stringparentid) { DataViewdv=dt.DefaultView; dv.RowFilter=parentid+"=0"; foreach(DataRowViewdrvindv) { TreeNodenode=newTreeNode(); node.Text=drv[text].ToString(); node.Value=drv[value].ToString(); node.Expanded=false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv,node,text,value,parentid); } } ///<summary> ///递归绑定子节点 ///</summary> ///<paramname="dv">源DataView</param> ///<paramname="parentNode">当前节点</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> privatestaticvoidCreatTreeViewChildNode(DataViewdv,TreeNodeparentNode,stringtext,stringvalue,stringparentid) { dv.RowFilter=parentid+"="+parentNode.Value; foreach(DataRowViewrowindv) { TreeNodereplyNode=newTreeNode(); replyNode.Text=row[text].ToString(); replyNode.Value=row[value].ToString(); replyNode.Expanded=false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv,replyNode,text,value,parentid); } } ///<summary> ///创建无限分级目录树TreeView ///</summary> ///<paramname="treeview">TreeView空间</param> ///<paramname="dt">数据源DataTable</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="url">url字段</param> ///<paramname="parentid">深度字段例如parentid</param> publicstaticvoidCreateLevelTreeView(TreeViewtreeview,DataTabledt,stringtext,stringvalue,stringurl,stringparentid) { DataViewdv=dt.DefaultView; dv.RowFilter=parentid+"=0"; foreach(DataRowViewdrvindv) { TreeNodenode=newTreeNode(); node.Text=drv[text].ToString(); node.Value=drv[value].ToString(); node.NavigateUrl=drv[url].ToString(); node.Expanded=false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv,node,text,value,url,parentid); } } ///<summary> ///递归绑定子节点 ///</summary> ///<paramname="dv">源DataView</param> ///<paramname="parentNode">当前节点</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="url">url字段</param> ///<paramname="parentid">深度字段例如parentid</param> privatestaticvoidCreatTreeViewChildNode(DataViewdv,TreeNodeparentNode,stringtext,stringvalue,stringurl,stringparentid) { dv.RowFilter=parentid+"="+parentNode.Value; foreach(DataRowViewrowindv) { TreeNodereplyNode=newTreeNode(); replyNode.Text=row[text].ToString(); replyNode.Value=row[value].ToString(); replyNode.NavigateUrl=row[url].ToString(); replyNode.Expanded=false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv,replyNode,text,value,url,parentid); } } #endregion #region创建无限分级ListBox ///<summary> ///创建无限分级ListBox ///</summary> ///<paramname="ddlst">ListBox控件</param> ///<paramname="dt">源DataTable</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> publicstaticvoidCreateLevelListBox(ListBoxddlst,DataTabledt,stringtext,stringvalue,stringparentid) { ArrayListallItems=newArrayList(); DataRow[]rows=dt.Select(parentid+"=0"); foreach(DataRowrowinrows) CreateLevelListBoxAssistant(dt,refallItems,row,string.Empty,text,value,parentid); ListItem[]items=newListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } ///<summary> ///递归绑定子节点 ///</summary> ///<paramname="dt">源DataTable</param> ///<paramname="items">数组</param> ///<paramname="parentRow">当前节点</param> ///<paramname="curHeader">前缀</param> ///<paramname="text">text字段</param> ///<paramname="value">value字段</param> ///<paramname="parentid">深度字段例如parentid</param> privatestaticvoidCreateLevelListBoxAssistant(DataTabledt,refArrayListitems,DataRowparentRow,stringcurHeader,stringtext,stringvalue,stringparentid) { ListItemnewItem=newListItem(curHeader+Until.CutString(parentRow[text].ToString(),18,true),parentRow[value].ToString()); items.Add(newItem); DataRow[]rows=dt.Select(parentid+"="+newItem.Value); for(inti=0;i<rows.Length-1;i++) CreateLevelListBoxAssistant(dt,refitems,rows[i],curHeader.Replace("┣","┃").Replace("┗","┣")+"┣",text,value,parentid); if(rows.Length>0) CreateLevelListBoxAssistant(dt,refitems,rows[rows.Length-1],curHeader.Replace("┣","┃").Replace("┗","┣")+"┗",text,value,parentid); } #endregion
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。