博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TreeView控件使用总结
阅读量:5057 次
发布时间:2019-06-12

本文共 5595 字,大约阅读时间需要 18 分钟。

刚开始接触TreeView控件的时候,感觉很苦恼,因为要像下面的方式来填充它:

using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class ascx_ReleaseMenuManage : System.Web.UI.UserControl{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            Dispaly();            GetParentNodes();                   }    }    private void Dispaly()    {        IList
parents = GetData("0"); foreach (Node parent in parents) { TreeNode parentnode = this.CreateTreeNode(parent.Name, parent.ID, parent.Url); CreateChildNode(parent.ID, parentnode); TreeView1.Nodes.Add(parentnode); } } private TreeNode CreateTreeNode(string funname, string id, string url) { TreeNode newNode = new TreeNode(); newNode.Text = funname; newNode.Value = id; //newNode.NavigateUrl = ResolveUrl(url.Trim()); return newNode; } private void CreateChildNode(string nodeid, TreeNode parentnode) { IList
children = GetData(nodeid); foreach (Node child in children) { TreeNode childnode = this.CreateTreeNode(child.Name, child.ID, child.Url); CreateChildNode(child.ID, childnode); AddTree(parentnode, childnode); } } private void AddTree(TreeNode fathernode, TreeNode chilenode) { fathernode.ChildNodes.Add(chilenode); } private IList
GetData(string id) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); SqlCommand cmd = new SqlCommand("select funid,funname,funurl from releasefuninfo where fatherid=@id", con); cmd.Parameters.AddWithValue("id", id); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); List
nodes = new List
(); while (reader.Read()) { Node n = new Node(); n.ID = reader["funid"].ToString(); n.Name = reader["funname"].ToString(); n.Url = reader["funurl"].ToString(); nodes.Add(n); } return nodes; } protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e) { string current = ((TreeView)sender).SelectedNode.Value; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); SqlCommand cmd = new SqlCommand("select funname,funurl,displayid from releasefuninfo where funid=@id", con); cmd.Parameters.AddWithValue("id", current); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { txtMenu_Name.Text = reader["funname"].ToString(); txtMenu_http.Text = reader["funurl"].ToString(); if (reader["displayid"].ToString() == "1") { chkMenu_Lock.Checked = true; } else { chkMenu_Lock.Checked = false; } } } private void GetParentNodes() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); SqlCommand cmd = new SqlCommand("select funid, funname from releasefuninfo where fatherid=0", con); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ListItem item=new ListItem(); item.Text = reader["funname"].ToString(); item.Value = reader["funid"].ToString(); txtMenu_FaterID.Items.Add(item); } con.Close(); } public class Node { public string ID { get; set; } public string Name { get; set; } public string Url { get; set; } }}

嘿嘿 ,最近终于找到一个简洁的方式来填充它了,很是开心,代码如下:

using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class TreeViewTest : System.Web.UI.Page{    DataSet ds ;    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            ds = new DataSet();           SqlConnection con = new SqlConnection(SqlHelp.Constr);        SqlDataAdapter sda = new SqlDataAdapter("select * from releasefuninfo", con);        sda.Fill(ds);             GridView1.DataSource = ds.Tables[0].DefaultView;        GridView1.DataBind();       AddNode(TreeView1.Nodes, "0");        }                  }    private void AddNode(TreeNodeCollection nodes, string id)    {        DataView dv = new DataView();        dv.Table = ds.Tables[0];        dv.RowFilter = "fatherid=" + "'" + id + "'";        TreeNode tn;        foreach (DataRowView v in dv)        {            tn = new TreeNode();            tn.Value = v["funid"].ToString();            tn.Text = v["funname"].ToString();            tn.NavigateUrl = ResolveUrl( v["funurl"].ToString());            nodes.Add(tn);            AddNode(tn.ChildNodes, tn.Value);        }    }}
类似的用法可以用子Menu控件上;

转载于:https://www.cnblogs.com/LittleFeiHu/archive/2011/02/27/1963524.html

你可能感兴趣的文章
在线视频播放软件
查看>>
用代码生成器生成的DAL数据访问操作类 基本满足需求了
查看>>
28初识线程
查看>>
Monkey测试结果分析
查看>>
Sublime Text 3 设置
查看>>
浅谈C++底层机制
查看>>
STL——配接器、常用算法使用
查看>>
第9课 uart
查看>>
Range和xrange的区别
查看>>
BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)
查看>>
java-动态规划算法学习笔记
查看>>
STL容器之vector
查看>>
Linux 内核中断内幕
查看>>
DNS负载均衡
查看>>
无法向会话状态服务器发出会话状态请求
查看>>
数据中心虚拟化技术
查看>>
Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
查看>>
01入门
查看>>
python正则表达式
查看>>
嵌套循环连接(nested loops join)原理
查看>>