如何用ASPxTreeView建立三级树(显示及数据绑定)

示例如图:三级树效果图

 

//设置treeviw默认为第一个菜单打开
if (ASPxTreeView1.SelectedNode == null)
ASPxTreeView1.SelectedNode = ASPxTreeView1.Nodes[0];

前端控件代码:前端代码控件图

<dx:ASPxTreeView ID="ASPxTreeView1" ClientInstanceName="ASPxTreeView1" runat="server" Width="100%" AllowSelectNode="True" AutoPostBack="false" Styles-NodeText-Font-Size="10px">
<ClientSideEvents Init="TreeNodeClick" NodeClick="TreeNodeClick" />
</dx:ASPxTreeView>

 

后天代码:

//数据绑定(重点)

#region 建立树状菜单

// 创建主菜单
private void CreateMenu()
{
//MainFrameState mainFrameState = new MainFrameState();
//d_loginUserId = mainFrameState.LoginUserID;
DataTable dt = new DataTable();
dt = platbll.GetB1tree().Tables[0];

if (dt.Rows.Count > 0)
{
DataRow[] mainRow = (DataRow[])dt.Select();
foreach (DataRow row in mainRow)
{

_treenode = new TreeViewNode();

_treenode.Text = row["PEONAME"].ToString();
_treenode.ToolTip = row["treepeoid"].ToString();// 储存一个id
ASPxTreeView1.Nodes.Add(_treenode);
if (row["treepeoid"].ToString()!=null)
{
CreateChildMenu(int.Parse(row["treepeoid"].ToString()), _treenode);
}

}
}

}
// B1级菜单
private void CreateChildMenu(int d_parentID, TreeViewNode TVN)
{
DataTable dt = new DataTable();
dt = platbll.GetB2tree(d_parentID).Tables[0];
DataRow[] childRow = (DataRow[])dt.Select();
foreach (DataRow row in childRow)
{
_treenode = new TreeViewNode();

_treenode.Text = row["PEONAME"].ToString();
_treenode.ToolTip = row["treepeoid"].ToString();
TVN.Nodes.Add(_treenode);
// ASPxTreeView1.Nodes.Add(TVN);
if (row["treepeoid"].ToString() != null)
{
CreateChildMenu1(int.Parse(row["treepeoid"].ToString()), _treenode);
}
}
}

// B2、B3级菜单
private void CreateChildMenu1(int d_parentID, TreeViewNode TVN)
{
DataTable dt = new DataTable();
dt = platbll.GetB2tree(d_parentID).Tables[0];
DataRow[] childRow = (DataRow[])dt.Select();
foreach (DataRow row in childRow)
{
_treenode = new TreeViewNode();

_treenode.Text = row["PEONAME"].ToString();
_treenode.ToolTip = row["treepeoid"].ToString();
TVN.Nodes.Add(_treenode);

}
}
#endregion

 

posted @ 2015-12-27 14:32  小鹿的无名书  阅读(1211)  评论(0编辑  收藏  举报