ASP.NET - TreeView(1)控件简单数据绑定

TreeView控件简单的动态数据绑定
(1)TreeView控件HTML代码
<
asp:TreeView ID="TreeViewInfo" runat="server" ExpandDepth="2" OnSelectedNodeChanged="TreeViewInfo_SelectedNodeChanged"

ImageSet="Msdn" NodeIndent="10">

<ParentNodeStyle Font-Bold="False" />

<HoverNodeStyle ForeColor="#4f72ac" BackColor="#efefef" />

<SelectedNodeStyle Font-Underline="False" BackColor="#efefef" />

<NodeStyle Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="1px"

VerticalPadding="2px" Font-Names="Verdana" />

</asp:TreeView>
(2)服务器代码

protected void Page_Load(object sender, EventArgs e)

{

                if (!IsPostBack)

               { CreateTreeView();}//加载事件中调用方法

}
//利用递归动态的绑定TreeView控件

private void CreateTreeView()

{

 

            EmployeesManager EmpManager = new EmployeesManager();

            DataTable dt = EmpManager.GetAllEmployee();//将所用的数据查找出来

            CreateTreeViewRecursive(TreeViewEmpInfo.Nodes, dt, 0);//绑定TreeView控件

}

private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSource, int parentId)

{

string fliter = String.Format("EmployeeMgrId={0}", parentId);

DataRow[] drArr = dataSource.Select(fliter);//动态查找出下一节点的数据

TreeNode node;

foreach (DataRow dr in drArr)

{

node = new TreeNode();

nodes.Add(node);

node.Text =dr["employeename"].ToString();

node.Value = dr["EmployeeId"].ToString();

CreateTreeViewRecursive(node.ChildNodes, dataSource, (int)dr["EmployeeId"]);

dataSource.Rows.Remove(dr);

}

}

posted @ 2009-08-21 17:07  张宏宇  阅读(1059)  评论(1编辑  收藏  举报