资源描述:
《vs2008 c# .net treeview 动态绑定数据库,增加、修改、删除节点.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、vs2008c#.nettreeview动态绑定数据库,增加、修改、删除节点假设在数据库中建立一张表,如tb_treeview,如下创建createtabletb_treeview(idintnotnull,displayNamevarchar(20),parentIdint,urlvarchar(100));向表中插入几个数据如:insertintotb_treeview(1,'中国',0,'');insertintotb_treeview(2,'安徽省',1,'');insertintotb_treeview(3,'合肥市',2,'');insertintotb_treevie
2、w(4,'江苏省',1,'');insertintotb_treeview(5,'南京市',4,'');insertintotb_treeview(6,'湖北省',1,'');作为演示数据。(1)、打开VS2008,新建一个网站项目,在Default.aspx中添加控件如下(2)、在Default.aspx.cs文件里添加如下代码:usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web
3、.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Data.SqlClient;usingSystem.Windows.Forms;usingSystem.Drawing;usingSystem.Data.OleDb;namespaceWebApplication{publicpartialclassWeb
4、Form3:System.Web.UI.Page{DataTabledt=newDataTable();protectedvoidPage_Load(objectsender,EventArgse){stringconString="DataSource=.;uid=sa;pwd=sa;Database=Arm";SqlConnectioncon=newSqlConnection(conString);try{Label4.Text="";if(!IsPostBack)//页面首次加载{con.Open();stringstrSQL="select*fromtb_treevieww
5、hereparentId='0'";SqlDataAdapterda=newSqlDataAdapter(strSQL,con);da.Fill(dt);AddTreeNode(0,(System.Web.UI.WebControls.TreeNode)null);}else{//con.Open();//stringstrSQL="select*fromtb_treeviewwhereparentId<>'0'";//SqlDataAdapterda=newSqlDataAdapter(strSQL,con);//da.Fill(dt);TreeView_SelectedInde
6、xChanged();}}//catch(Exceptione)//{//}finally{con.Close();}}protectedvoidAddTreeNode(intparentId,System.Web.UI.WebControls.TreeNodepNode){DataViewdv=newDataView(dt);//过滤parentId,得到当前节点的所有子节点dv.RowFilter="parentId="+parentId;foreach(DataRowViewdrvindv){if(parentId==0){System.Web.UI.WebControls.
7、TreeNodetn1=newSystem.Web.UI.WebControls.TreeNode();tn1.Text=drv["displayName"].ToString();//节点上要显示的名称tn1.Value=drv["id"].ToString();tn1.NavigateUrl=drv["url"].ToString();//点击节点名称,跳转到指定url页面TreeView1.Nodes.Add(tn1);//将根节点加入到TreeView中去tn