我相信很多.NET新人会对TreeView非常感兴趣,因为TreeView功能十分强大,而且在应用中也经常出现,包括一些框架的设计上可能也会采用TreeView的。
       废话不多说了,我从第一步讲起。
 
一、首先在界面上拉一个TreeView、一个Button、一个Label。TreeView的格式和风格都可以自己调整。(上图是我项目的截图)
 
二、数据库。包含两张表big 和 class
big:
 
class:
 
三、进入后台,下面是后台代码。代码里有注释。
InBlock.gif
using System; 

InBlock.gif
using System.Configuration; 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Web; 

InBlock.gif
using System.Web.Security; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web.UI.HtmlControls; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI.WebControls.WebParts; 

InBlock.gif
using System.Data.SqlClient; 

InBlock.gif 

InBlock.gif
public partial 
class _Default : System.Web.UI.Page    

InBlock.gif

InBlock.gif        
protected 
void Page_Load(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif                
if (!IsPostBack) 

InBlock.gif                { 

InBlock.gif                        
//填充第一级节点 

InBlock.gif                        DataView dv = drv(
"select * from big"); 

InBlock.gif                        
foreach (DataRowView datav 
in dv) 

InBlock.gif                        {
//循环数据,读取到节点上 

InBlock.gif                                TreeNode tn = 
new TreeNode(); 

InBlock.gif                                tn.Text = datav[
"title"].ToString(); 

InBlock.gif                                tn.Value = datav[
"bid"].ToString(); 

InBlock.gif                                tn.Expanded = 
false
//当前节点都是折叠起来的 

InBlock.gif                                TreeView1.Nodes.Add(tn); 
//填充 

InBlock.gif                                AddchildNodes(tn); 
//调用方法填充二级节点 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif        
private DataView drv(
string sql) 

InBlock.gif        { 

InBlock.gif                
string conn = 
"Server=.;User id=sa;Pwd=L;Database=test";    
//这个是我本地的数据库命令 

InBlock.gif                SqlConnection con = 
new SqlConnection(conn); 

InBlock.gif                SqlDataAdapter sda = 
new SqlDataAdapter(sql, con); 

InBlock.gif                DataSet ds = 
new DataSet(); 

InBlock.gif                sda.Fill(ds, 
"tree"); 

InBlock.gif                
return ds.Tables[
"tree"].DefaultView;    
//注意一下这里的返回值 

InBlock.gif        } 

InBlock.gif        
private 
void AddchildNodes(TreeNode tn) 

InBlock.gif        {
//二级节点 

InBlock.gif                
int bid = Convert.ToInt32(tn.Value); 

InBlock.gif                DataView dv = drv(
"select * from class where bid = " + bid + ""); 

InBlock.gif                
foreach (DataRowView datav 
in dv) 

InBlock.gif                { 

InBlock.gif                        TreeNode ctn = 
new TreeNode(); 

InBlock.gif                        ctn.Text = datav[
"title"].ToString(); 

InBlock.gif                        ctn.Value = datav[
"cid"].ToString(); 

InBlock.gif                        ctn.NavigateUrl = 
"http://mixangel.blog.51cto.com/"; 
InBlock.gif                        tn.ChildNodes.Add(ctn);    //加载到tn的下级节点 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif        protected void Button1_Click(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                //按钮事件,根据check选定与否来显示 
InBlock.gif                if (TreeView1.CheckedNodes.Count > 0) 
InBlock.gif                { 
InBlock.gif                        foreach (TreeNode tn in TreeView1.CheckedNodes) 
InBlock.gif                        { 
InBlock.gif                                if (tn.ChildNodes.Count > 0) 
InBlock.gif                                { 
InBlock.gif                                        foreach (TreeNode ctn in tn.ChildNodes) 
InBlock.gif                                        { 
InBlock.gif                                                Response.Write(ctn.Text + "<br>"); 
InBlock.gif                                        } 
InBlock.gif                                } 
InBlock.gif                                else 
InBlock.gif                                { 
InBlock.gif                                        Response.Write(tn.Text + "<br>"); 
InBlock.gif                                } 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        //下面是TreeView的三个事件,非常简单,就是点击TreeView触发的事件,代码实现后可以看看效果。 
InBlock.gif        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Label1.Text = TreeView1.SelectedNode.Text; 
InBlock.gif        } 
InBlock.gif        protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e) 
InBlock.gif        { 
InBlock.gif                Label1.Text = "你折叠了" + e.Node.Text; 
InBlock.gif        } 
InBlock.gif        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e) 
InBlock.gif        { 
InBlock.gif                Label1.Text = "你展开了" + e.Node.Text; 
InBlock.gif        } 
InBlock.gif
 
 
四、实现效果图。
 
       TreeView是很多管理软件都会用到的一个控件,功能也十分强大,能导航能链接,虽然不是出镜率非常多,但是一般办公类软件都会有这个控件。
       希望以后和大家多交流.NET的控件方面的东西,我觉得可能创意是很重要的,其实控件本身很简单,虽然现在.NET的控件还是有很多鸡肋的东西,但  是据我了解,微软的silverlight已经给中国软件业很大的惊喜了,以后估计会是不可忽视的一股冲击力。
       希望以后能和大家沟通和交流。