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

public partial class _Default : System.Web.UI.Page    
{
        protected void Page_Load(object sender, EventArgs e)
        {
                if (!IsPostBack)
                {
                        //填充第一级节点
                        DataView dv = drv("select * from big");
                        foreach (DataRowView datav in dv)
                        {//循环数据,读取到节点上
                                TreeNode tn = new TreeNode();
                                tn.Text = datav["title"].ToString();
                                tn.Value = datav["bid"].ToString();
                                tn.Expanded = false; //当前节点都是折叠起来的
                                TreeView1.Nodes.Add(tn); //填充
                                AddchildNodes(tn); //调用方法填充二级节点
                        }
                }
        }
        private DataView drv(string sql)
        {
                string conn = "Server=.;User id=sa;Pwd=L;Database=test";    //这个是我本地的数据库命令
                SqlConnection con = new SqlConnection(conn);
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "tree");
                return ds.Tables["tree"].DefaultView;    //注意一下这里的返回值
        }
        private void AddchildNodes(TreeNode tn)
        {//二级节点
                int bid = Convert.ToInt32(tn.Value);
                DataView dv = drv("select * from class where bid = " + bid + "");
                foreach (DataRowView datav in dv)
                {
                        TreeNode ctn = new TreeNode();
                        ctn.Text = datav["title"].ToString();
                        ctn.Value = datav["cid"].ToString();
                        ctn.NavigateUrl = "http://mixangel.blog.51cto.com/";
                        tn.ChildNodes.Add(ctn);    //加载到tn的下级节点
                }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
                //按钮事件,根据check选定与否来显示
                if (TreeView1.CheckedNodes.Count > 0)
                {
                        foreach (TreeNode tn in TreeView1.CheckedNodes)
                        {
                                if (tn.ChildNodes.Count > 0)
                                {
                                        foreach (TreeNode ctn in tn.ChildNodes)
                                        {
                                                Response.Write(ctn.Text + "<br>");
                                        }
                                }
                                else
                                {
                                        Response.Write(tn.Text + "<br>");
                                }
                        }
                }
        }

        //下面是TreeView的三个事件,非常简单,就是点击TreeView触发的事件,代码实现后可以看看效果。
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
                Label1.Text = TreeView1.SelectedNode.Text;
        }
        protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
                Label1.Text = "你折叠了" + e.Node.Text;
        }
        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
                Label1.Text = "你展开了" + e.Node.Text;
        }
}
 
 
四、实现效果图。
 
       TreeView是很多管理软件都会用到的一个控件,功能也十分强大,能导航能链接,虽然不是出镜率非常多,但是一般办公类软件都会有这个控件。
       希望以后和大家多交流.NET的控件方面的东西,我觉得可能创意是很重要的,其实控件本身很简单,虽然现在.NET的控件还是有很多鸡肋的东西,但  是据我了解,微软的silverlight已经给中国软件业很大的惊喜了,以后估计会是不可忽视的一股冲击力。
       希望以后能和大家沟通和交流。