爱晚红枫技术部广东分部








永远自由的心

利用.net 2.0中的TreeView控件与数据库绑定,生成无限级的树目录

数据表的结构

www.lemongtree.com 
    
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
public partial class _Default : System.Web.UI.Page 

    
protected string _connstr = ConfigurationManager.ConnectionStrings["DemoConnectionstrings"].ConnectionString; 
    
protected void Page_Load(object sender, EventArgs e) 
    

        
if (!Page.IsPostBack) 
        

            BindTree(); 
            
using (SqlConnection _conn = new SqlConnection(_connstr)) 
            

                SqlCommand _comm 
= new SqlCommand("select * from treeview", _conn); 

                _conn.Open(); 
                
using (SqlDataReader r = _comm.ExecuteReader()) 
                

                    
while (r.Read()) 
                    

                        ListItem item 
= new ListItem(r["txt"].ToString(), r["id"].ToString()); 
                        
this._dd_parent.Items.Add(item); 
                    }
 
                }
 
            }
 
            
this._dd_parent.Items.Insert(0new ListItem(".""0")); 
        }
 
    }
 
    
protected void BindTree() 
    

        TreeNode node 
= new TreeNode();   //这里是创建一个根节点,就是dome中看到的Root 
        node.Text = "Root"
        CreateChildTree(node, 
0);    
        _tree_view.Nodes.Add(node); 
    }
 

    
protected void CreateChildTree(TreeNode _parentNode, int _parentID) 
    

        
using (SqlConnection _conn = new SqlConnection(_connstr)) 
        

            SqlCommand _comm 
= new SqlCommand(); 
            
string sql = "select * from treeview where rootid=@rootid"
            _comm.Parameters.Add(
"@rootid", SqlDbType.Int).Value = _parentID; 
            _comm.CommandText 
= sql; 
            _comm.Connection 
= _conn; 
            _conn.Open(); 
            
using (SqlDataReader r = _comm.ExecuteReader()) 
            

                
while (r.Read()) 
                

                    TreeNode _node 
= new TreeNode(r["txt"].ToString()); 
                        CreateChildTree(_node, (
int)r["ID"]);  //递归出子节点 
                        _parentNode.ChildNodes.Add(_node); 
                }
 
            }
 
        }
 
    }
 
    
protected void _btn_submit_Click(object sender, EventArgs e) 
    

        
int _rootid = Convert.ToInt16(this._dd_parent.SelectedValue); 
        
string _txt = this._txt_txt.Text.Trim(); 
        
using (SqlConnection _conn = new SqlConnection(_connstr)) 
        

            SqlCommand _comm 
= new SqlCommand("insert into treeview (txt,rootid) values (@txt,@id)", _conn); 
            _comm.Parameters.Add(
"@txt", SqlDbType.VarChar, 50).Value = _txt; 
            _comm.Parameters.Add(
"@id", SqlDbType.Int).Value = _rootid; 
            _conn.Open(); 
            _comm.ExecuteNonQuery(); 
        }
 
        
this._tree_view.Nodes.Clear(); 
        BindTree(); 
    }
 
}
 

posted on 2006-07-13 16:08 e旋风 阅读(3275) 评论(5)  编辑 收藏 网摘 所属分类: ASP.NET

评论

#1楼  2007-03-15 09:06 janvy [未注册用户]

急问,._dd_parent的定义是什么?~~~   回复  引用    

#2楼  2008-04-14 21:13 蜗牛身上的一只蚂蚁      

看得不清不楚啊,大大有没有源代码看看啊。谢谢啊。MSN:cancanliunx_c110@hotmail.com
邮件:
yjjepl110@163.com,谢谢大大啊。   回复  引用  查看    

#3楼  2008-05-29 15:11 Joyaspx      

你的这个方法不是很好,需要多次查询数据库,效率低   回复  引用  查看    

#4楼  2008-08-09 18:10 fg [未注册用户]

sdfdsfdsfdsfsdfdfsd   回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接:
 

导航

统计

公告

人所拥有的「最后的」(last)自由是,
我们可以选择我们的态度。
遭遇同样的打击,
有的人选择的是绝望,
有的人却选择了希望



与我联系

搜索

 

常用链接

留言簿(4)

我的标签

随笔分类

文章分类

相册

收藏夹

Web标准

Web开发

xml学习

博客集合

图客

网站收藏夹

我的邻居

最新随笔

最新评论

阅读排行榜

评论排行榜