asp.net 2.0中TreeView(无限级)绑定数据库
数据库结构如图:
![](https://www.cnblogs.com/images/cnblogs_com/sunfishlu/97376/r_%e6%9c%aa%e5%91%bd%e5%90%8d.bmp)
关键代码:
![](https://www.cnblogs.com/images/cnblogs_com/sunfishlu/97376/r_%e6%9c%aa%e5%91%bd%e5%90%8d.bmp)
关键代码:
1
protected 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)