TreeView Populate On Demand
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PopulateOnDemand" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>TreeView Populate On Demand</title>
<style type="text/css">
h2 { background-color: #A6B4DB; width: 300px; padding: 4px; border: solid 1pt #A6B4DB;}
.tree { width: 300px; padding: 4px; border: solid 1pt #A6B4DB;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<h2>Product Nodes Filled On Demand</h2>
<asp:TreeView ID="treeMain"
runat="server"
CssClass="tree"
OnTreeNodePopulate="treeMain_TreeNodePopulate"
ExpandDepth="1"
EnableClientScript="true"
PopulateNodesFromClient="true">
</asp:TreeView>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PopulateOnDemand : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] groups = ProductCatalog.GetGroups();
foreach (string grp in groups)
{
TreeNode groupNode = new TreeNode(grp);
string[] subheadings = ProductCatalog.GetSubHeadingsByGroup(grp);
foreach (string sub in subheadings)
{
TreeNode subheadingNode = new TreeNode(sub);
subheadingNode.PopulateOnDemand = true;
groupNode.ChildNodes.Add(subheadingNode);
}
treeMain.Nodes.Add(groupNode);
}
}
}
protected void treeMain_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
TreeNode subHeadingNode = e.Node;
string sub = subHeadingNode.Value;
string[] products = ProductCatalog.GetProductsBySubHeading(sub);
foreach (string prod in products)
{
TreeNode productNode = new TreeNode(prod);
subHeadingNode.ChildNodes.Add(productNode);
}
}
}
class ProductCatalog
{
private static Product[] _products = {
new Product(1, "A", "7", 39.99),
new Product(2, "B", "8", 49.99),
new Product(3, "C", "7", 39.99),
new Product(4, "D", "8", 49.99),
new Product(5, "E", "7", 39.99),
new Product(6, "F", "8", 49.99),
new Product(7, "G", "7", 39.99),
new Product(8, "H", "8", 49.99),
new Product(9, "I", "7", 39.99),
new Product(10, "J", "8", 49.99),
new Product(11, "K", "7", 39.99),
new Product(12, "L", "8", 49.99),
};
private static string[] _groups = { "Categories", "Series" };
private static string[] _subheadings1 = { "Graphics", "Internet", };
private static string[] _subheadings2 = { "Core Series", ".NET Series" };
private static string[] _books1 = { "GDI" };
private static string[] _books2 = { "HTML", "CSS", "Javascript", "Web Services" };
private static string[] _books3 = { "Java", "C#", "CSS" };
private static string[] _books4 = { "C++", "Web Services" };
private static Product[] _prods1 = { _products[0] };
private static Product[] _prods2 = { _products[5], _products[6], _products[7], _products[3] };
private static Product[] _prods3 = { _products[0], _products[1], _products[7] };
private static Product[] _prods4 = { _products[2], _products[3] };
public static Product[] GetProducts()
{
return _products;
}
public static Product GetProductById(int id)
{
foreach (Product prod in _products)
{
if (prod.Id == id)
return prod;
}
return null;
}
public static string[] GetGroups()
{
return _groups;
}
public static string[] GetSubHeadingsByGroup(string group)
{
if (group == "Categories")
return _subheadings1;
else
return _subheadings2;
}
public static string[] GetProductsBySubHeading(string subheading)
{
if (subheading == "Graphics")
return _books1;
else if (subheading == "Internet")
return _books2;
else if (subheading == "Core Series")
return _books3;
else
return _books4;
}
public static Product[] GetProductObjectsBySubHeading(string subheading)
{
if (subheading == "Graphics")
return _prods1;
else if (subheading == "Internet")
return _prods2;
else if (subheading == "Core Series")
return _prods3;
else
return _prods4;
}
}
public class Product
{
public int Id;
public string Name;
public string Isbn;
public double Price;
public Product(int id, string name, string isbn)
{
Id = id;
Name = name;
Isbn = isbn;
Price = 0.0;
}
public Product(int id, string name, string isbn, double price)
{
Id = id;
Name = name;
Isbn = isbn;
Price = price;
}
}
Related examples in the same category