ASP.NET基础教程-TreeView树控件-给TreeView控件添加结点
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://chenxing.blog.51cto.com/240526/58172 |
一、引用命名空间
using Microsoft.Web.UI.WebControls; 二、在表单的Load加载事件中代码如下:
//创建数据库连接
con=new SqlConnection(@ConfigurationSettings.AppSettings["server"]); //打开数据库连接 con.Open();
//SQL语句
string oSql="select mkmc,imageurl,akey,navigateurl from userqxb where bkey='0_' and enabled=1 and id='"+Session["id"].ToString()+"'"; //连接数据库提取数据
SqlDataAdapter da=new SqlDataAdapter(oSql,con); //创建记录集
DataSet ds=new DataSet();
//将取出的数据加载到记录集
da.Fill (ds,"root");//将装有根结点的表取入内存数据集中 //关闭数据库连接
con.Close (); //循环从记录集中取出数据作为树控件的主结点 for(int i=0;i<ds.Tables ["root"].Rows .Count;i++ ){ TreeNode node=new TreeNode ();//建一个结点对象 node.Text =ds.Tables ["root"].Rows [i][0].ToString (); node.ImageUrl=@ds.Tables ["root"].Rows [i][1].ToString (); node.DefaultStyle.CssText="color:Green;font-size:10pt;font-weight:bold;font-name:Arial"; node.SelectedStyle.CssText="color:Yellow;font-name:Arial;font-weight:bold"; TreeView.Nodes .Add (node);//向根结点集合中加入该根结点 con.Open (); oSql="select mkmc,imageurl,navigateurl from userqxb where bkey='"+ds.Tables ["root"].Rows [i][2].ToString ()+"' and id='"+Session["id"].ToString()+"' and enabled=1"; comm=new SqlCommand (oSql,con); SqlDataReader dr=comm.ExecuteReader ();//从装有子结点的表中读取子结点 while(dr.Read ()) { TreeNode child=new TreeNode();//建一个结点对象,作为子结点 child.Text =dr.GetString (dr.GetOrdinal ("mkmc")); child.ImageUrl=@dr.GetString (dr.GetOrdinal ("imageurl")); child.NavigateUrl=@dr.GetString (dr.GetOrdinal ("navigateurl")); child.Target="main"; child.DefaultStyle.CssText="color:DodgerBlue;font-size:9pt;font-weight:bold;font-name:Arial"; child.SelectedStyle.CssText="color:Yellow;font-name:Arial;font-weight:bold"; TreeView.Nodes [i].Nodes .Add (child);//将子结点加到根结点下 } //TreeView.Nodes[i].Expanded=true;//将第一个根结点展开 dr.Close (); con.Close();} TreeView.Nodes[0].Expanded=true;//将第一个根结点展开 TreeView.SelectedNodeIndex="0.0";//设置选定结点的索引值 ![]() 本文出自 “半瓶香油-好男人时来运转” 博客,请务必保留此出处http://chenxing.blog.51cto.com/240526/58172 本文出自 51CTO.COM技术博客 |



晨星
博客统计信息
热门文章
最新评论
友情链接

