treeview构造函数,以备日后使用
需要首先获取树的datatable,该表结构是有父子关系 的表,另外,该方法中未将target进行设置,如有需要可以再加参数,并将树节点的target属性赋值即可指定节点url对应的frame public class Tree { public Tree() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 构造树 /// </summary> /// <param name="tv">所需构造的树</param> /// <param name="pNode">需添加的节点,如为空则构造完整树</param> /// <param name="dt_treedata">构造数datatable</param> /// <param name="idCol">关键字列名</param> /// <param name="parentCol">上级编码列</param> /// <param name="textCol">文本列</param> /// <param name="urlcol">对应url</param> /// <param name="ParentID">上级节点id</param> public static void BuildTree(TreeView tv, TreeNode pNode, DataTable dt_treedata, string idCol, string parentCol, string textCol, string urlcol, string ParentID) { string oldfilter = dt_treedata.DefaultView.RowFilter; DataView dvTree = new DataView(dt_treedata); string Fstr = ""; if (ParentID == "") Fstr = parentCol + " is null "; else Fstr = parentCol + " = '" + ParentID + "'"; if (string.IsNullOrEmpty(oldfilter)) dvTree.RowFilter = Fstr; else dvTree.RowFilter = oldfilter + " and " + Fstr; foreach (DataRowView Row in dvTree) { TreeNode Node = new TreeNode(); if (pNode == null) { //Node.Name = Row[idCol].ToString(); Node.Value = Row.Row[idCol].ToString(); Node.Text = Row.Row[textCol].ToString(); Node.NavigateUrl = Row.Row[urlcol].ToString(); tv.Nodes.Add(Node); BuildTree(tv, Node, dt_treedata, idCol, parentCol, textCol, urlcol, Row.Row[idCol].ToString()); } else //处理子节点 { //Node.Name = Row[idCol].ToString(); Node.Value = Row.Row[idCol].ToString(); Node.Text = Row.Row[textCol].ToString(); Node.NavigateUrl = Row.Row[urlcol].ToString(); pNode.ChildNodes.Add(Node); BuildTree(tv, Node, dt_treedata, idCol, parentCol, textCol, urlcol, Row.Row[idCol].ToString()); } } } } 调用方法如下: public void BindTree() { DataTable dt_treedata = GetClassfifyData(); this.TreeView1.Nodes.Clear(); WebFrameWork.WebControls.Tree.BuildTree(this.TreeView1, null, dt_treedata, "ID", "sparentid", "sname", "url", ""); } public DataTable GetClassfifyData() { DataTable dt_temp = new DataTable(); dt_temp.Columns.Add("ID",typeof(string)); dt_temp.Columns.Add("sparentid", typeof(string)); dt_temp.Columns.Add("sname", typeof(string)); dt_temp.Columns.Add("url", typeof(string)); WebFrameWork.BF.BF_FileClassify bf_file = new WebFrameWork.BF.BF_FileClassify(); QueryParam qp = new QueryParam(); int rowcount = 0; qp.OrderType = 0; ArrayList al = bf_file.GetList(qp, out rowcount); foreach (FL_CLASSIFYTable fc in al) { dt_temp.Rows.Add(fc.ID, fc.SPARENTID, fc.SNAME, "../filemanager/list.aspx?classifyid='" + fc.ID + "'"); } return dt_temp; }
posted on 2008-06-19 16:13 oldkingsir 阅读(227) 评论(0) 编辑 收藏 举报