Nella prima parte viene indicato l'oggetto nella pagina, nella seconda il codice da integrare per il caricamento di esempio.
Parte da integrare nella pagina .aspx
<asp:TreeView ID="treeview_example" runat="server" Width="220" ShowCheckBoxes="Leaf"> <DataBindings> <asp:TreeNodeBinding DataMember="siteMapNode" TextField="title" ValueField="resourceKey" SelectAction="None" /> </DataBindings> </asp:TreeView>Codice da integrare nel file .aspx.cs
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { PopulateTreeView(); } protected void PopulateTreeView() { DataTable dataTable = null; try { dataTable = new DataTable("dt"); dataTable.Columns.Add(new DataColumn("value", typeof(int))); dataTable.Columns.Add(new DataColumn("area", typeof(string))); dataTable.Columns.Add(new DataColumn("type", typeof(string))); DataRow dataRow = dataTable.NewRow(); dataRow = dataTable.NewRow(); dataRow["value"] = "1"; dataRow["area"] = "Example root"; dataRow["type"] = "Check 1"; dataTable.Rows.Add(dataRow); dataRow = dataTable.NewRow(); dataRow["value"] = "2"; dataRow["area"] = ""; dataRow["type"] = "Check 2"; dataTable.Rows.Add(dataRow); dataRow = dataTable.NewRow(); dataRow["value"] = "3"; dataRow["area"] = ""; dataRow["type"] = "Check 3"; dataTable.Rows.Add(dataRow); /*...*/ TreeNode rootNode = new TreeNode(); if ((dataTable != null) && (dataTable.Rows.Count != 0)) { DataView dataView = new DataView(dataTable); foreach (DataRowView r in dataView) { if ("" != (string)r["area"]) rootNode = AddNode("", (string)r[1].ToString(), "Folder", treeview_example.Nodes); AddNode(r[0].ToString(), (string)r[2].ToString(), "Page", rootNode.ChildNodes); } } else { //...do something } } catch (Exception ex) { //log.Error(ex); } if (dataTable != null) dataTable.Dispose(); } private TreeNode AddNode(string id, string text, string type, TreeNodeCollection nodeCollection) { TreeNode node = new TreeNode(id); node.Value = id; node.Text = text; nodeCollection.Add(node); return node; } }Output: