asp.net 2.0中TreeView(无限级)绑定数据库
数据库结构如图:
关键代码:
关键代码:
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 DataBindHelper.BindDropDown(this.DropDownList1, News_LocationDAL.Instance.GetList().Tables[0], "PK_Location", "LocationName");
6 TreeNode rootnode = new TreeNode("新闻栏目", "0", "", "newscolright.aspx", "NewsColRight");
7 rootnode.Expanded = true;
8 TreeView1.Nodes.Add(rootnode);
9 CreateTree("0", rootnode);
10 }
11 }
12 private void CreateTree(string belong, System.Web.UI.WebControls.TreeNode rootnode)
13 {
14 DataSet ds = News_ColumnDAL.Instance.GetListByPid(belong,this.DropDownList1.SelectedValue);
15
16 foreach (DataRow dr in ds.Tables[0].Rows)
17 {
18 TreeNode treenode = new TreeNode("", "", "", "newscolright.aspx", "NewsColRight");
19 treenode.Text = dr["NewsColName"].ToString().Trim();
20 treenode.Expanded = true;
21 rootnode.ChildNodes.Add(treenode);
22
23 string id = dr["PK_NewsCol"].ToString().Trim();
24 CreateTree(id, treenode);
25 TreeNodeCollection childNodes = rootnode.ChildNodes;
26 childNodes[childNodes.Count - 1].NavigateUrl = "newscolright.aspx?NewsColID=" + dr["PK_NewsCol"].ToString() + "&NewsColPID=" + dr["NewsPID"] + "&PK_Location=" + dr["PK_Location"].ToString();
27 }
28 }
2 {
3 if (!IsPostBack)
4 {
5 DataBindHelper.BindDropDown(this.DropDownList1, News_LocationDAL.Instance.GetList().Tables[0], "PK_Location", "LocationName");
6 TreeNode rootnode = new TreeNode("新闻栏目", "0", "", "newscolright.aspx", "NewsColRight");
7 rootnode.Expanded = true;
8 TreeView1.Nodes.Add(rootnode);
9 CreateTree("0", rootnode);
10 }
11 }
12 private void CreateTree(string belong, System.Web.UI.WebControls.TreeNode rootnode)
13 {
14 DataSet ds = News_ColumnDAL.Instance.GetListByPid(belong,this.DropDownList1.SelectedValue);
15
16 foreach (DataRow dr in ds.Tables[0].Rows)
17 {
18 TreeNode treenode = new TreeNode("", "", "", "newscolright.aspx", "NewsColRight");
19 treenode.Text = dr["NewsColName"].ToString().Trim();
20 treenode.Expanded = true;
21 rootnode.ChildNodes.Add(treenode);
22
23 string id = dr["PK_NewsCol"].ToString().Trim();
24 CreateTree(id, treenode);
25 TreeNodeCollection childNodes = rootnode.ChildNodes;
26 childNodes[childNodes.Count - 1].NavigateUrl = "newscolright.aspx?NewsColID=" + dr["PK_NewsCol"].ToString() + "&NewsColPID=" + dr["NewsPID"] + "&PK_Location=" + dr["PK_Location"].ToString();
27 }
28 }