asp .Net TreeView实现数据绑定跟事件响应

asp .Net TreeView实现数据绑定和事件响应

最近做了一个图书馆管理系统,其中要实现中图法分类号查询,因为初学asp ,感觉还有点难度,

第一步:数据库文件

asp .Net TreeView实现数据绑定跟事件响应

第二步 向界面中拖进TreeView控件

第三步添加事件

下面是cs文件代码

 //TreeView绑定数据库
    public void BindDataBase(string sql){
        string str = System.Configuration.ConfigurationManager.
              ConnectionStrings["conn"].ToString();
        SqlConnection connection = new SqlConnection(str);
        try
        {
            connection.Open();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

        SqlCommand sqlCmd = new SqlCommand(sql, connection);

        SqlDataAdapter custDA = new SqlDataAdapter();
        custDA.SelectCommand = sqlCmd;
        DataSet ds = new DataSet();
        custDA.Fill(ds, "BookType");

        TreeNode root = new TreeNode("中图法分类");//新建根节点
        this.Kind_Tree.Nodes.Add(root);
        for (int i = 0; i < ds.Tables["BookType"].Rows.Count; i++)
        {   
            string parent=ds.Tables["BookType"].Rows[i][2].ToString();//获取数据库中标记节点
            if(parent=="root"){//判断是否是根节点下的子节点
                TreeNode tree2 = new TreeNode(ds.Tables["BookType"].Rows[i][0].ToString() 
                    + ds.Tables["BookType"].Rows[i][1].ToString());//创建一级子节点
            root.ChildNodes.Add(tree2);
           /*for (int j = 0; j < ds.Tables["BookType"].Rows.Count; j++)
            {
                string myparent = ds.Tables["BookType"].Rows[i][2].ToString();
                if (myparent == tree2.Text.ToString())
                {
                    TreeNode tree3 = new TreeNode(ds.Tables["BookType"].Rows[i][0].ToString() + ds.Tables["BookType"].Rows[i][1].ToString());
                    tree2.ChildNodes.Add(tree3);
                }
            }*/
            }
        }
        for (int j = 0; j < root.ChildNodes.Count; j++)
        {
            TreeNode rootChild = root.ChildNodes[j];//获取根节点下的子节点
            for (int k = 0; k < ds.Tables["BookType"].Rows.Count; k++)
            {
                string parent = ds.Tables["BookType"].Rows[k][2].ToString();//获取数据库中标记节点
               
                if (parent == rootChild.Text.ToString().Substring(0,1))//判断是否是二级子节点
                {
                    TreeNode tree3 = new TreeNode(ds.Tables["BookType"].Rows[k][0].ToString()
                        +"-"+ ds.Tables["BookType"].Rows[k][1].ToString());//新建二级子节点
                    rootChild.ChildNodes.Add(tree3);
                }
            }
        }

        //TreeNode rootChil = root.ChildNodes[0];
       // Response.Write(rootChil.Text.ToString().Substring(0,1));
            connection.Close();
    }

    //TreeView的响应事件


    protected void Kind_Tree_SelectedNodeChanged(object sender, EventArgs e)
    {
        string s = Kind_Tree.SelectedNode.ValuePath.ToString();
        char[] spitChar = new char[] { '/' };
        string[] nodeValues = s.Split(spitChar);
        string sql = null;
        if (nodeValues.Length == 2)
        {
            string typeId = nodeValues[1].Substring(0, 1);
             sql = "select 图书编号,出版社,作者,书名,图书ISBN号,分类 from bookTable d, BookType t where t.BoType_id like '"
                + typeId + "%' and t.BoType_name=d.分类";
        }
        else { 
             char[] spitChar2 = new char[] { '-' };
             string[] typeId2 = nodeValues[2].Split(spitChar2);
              sql = "select 图书编号,出版社,作者,书名,图书ISBN号,分类 from bookTable d, BookType t where t.BoType_id ='"
                 + typeId2[0] + "' and t.BoType_name=d.分类";
        }

        SqlDataSource1.SelectCommand = sql;
        Response.Write(nodeValues.Length);
    }
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {

           
                string sql = "select * from BookType";
                BindDataBase(sql);
                Kind_Tree.ShowLines = true;
                Kind_Tree.ExpandDepth = 2;

       }
}

 

显示时,因为节点的文本太长,如果要设置自动换行,还需要设置TreeView的属性

NodeWrap = true