刚开始接触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<Node> 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<Node> 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<Node> 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<Node> nodes = new List<Node>(); 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); } } }