<%@ Page Theme="Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Populating TreeView Nodes from a SQL Database</title>
</head>
<script language="C#" runat="server">
void GetProductCategories (TreeNode node) {
CategoryList categories = WarehouseDB.GetProductCategories();
foreach (Category c in categories) {
TreeNode newNode = new TreeNode(c.Name, c.Id);
newNode.SelectAction = TreeNodeSelectAction.Expand;
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNode);
}
}
void GetProductsForCategory (TreeNode node) {
String categoryId = node.Value;
ProductList products = WarehouseDB.GetProductsForCategory(categoryId);
foreach (Product p in products) {
TreeNode newNode = new TreeNode(p.Name, p.Id);
node.ChildNodes.Add(newNode);
}
}
void PopulateNode (Object source, TreeNodeEventArgs e) {
switch (e.Node.Depth) {
case 0 :
GetProductCategories(e.Node);
break;
case 1 :
GetProductsForCategory(e.Node);
break;
}
}
</script>
<body>
<div>
<h2>Populating TreeView Nodes from a SQL Database</h2>
<form action="treeview11_database_cs.aspx" runat="server">
<asp:TreeView ID="TreeView1" OnTreeNodePopulate="PopulateNode" SkinId="Simple" Width="250" ExpandDepth="0" runat="server">
<Nodes>
<asp:TreeNode Text="Inventory" SelectAction="Expand" PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
</form>
</div>
</body>
</html>
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
public
class WarehouseDB {
public
static CategoryList GetProductCategories() {
CategoryList categories =
new CategoryList();
String connStr = ConfigurationManager.ConnectionStrings["GrocerToGo"].ConnectionString;
SqlDataSource mySource =
new SqlDataSource(connStr, "GetCategories");
mySource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
IEnumerable result = mySource.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView row
in result) {
categories.Add(
new Category(
row["CategoryId"].ToString(),
row["CategoryName"].ToString())
);
}
return categories;
}
public
static ProductList GetProductsForCategory(String categoryId) {
ProductList products =
new ProductList();
String connStr = ConfigurationManager.ConnectionStrings["GrocerToGo"].ConnectionString;
SqlDataSource mySource =
new SqlDataSource(connStr, "GetProductsForCategory");
mySource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
mySource.SelectParameters.Add(
new Parameter("catId", TypeCode.Int32, categoryId));
IEnumerable result = mySource.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView row
in result) {
products.Add(
new Product(
row["ProductId"].ToString(),
row["ProductName"].ToString())
);
}
return products;
}
}
public
class Category {
public String Id;
public String Name;
public Category (String id, String name) {
this.Id = id;
this.Name = name;
}
}
public
class CategoryList : ArrayList {
public
new Category
this[
int i] {
get {
return (Category)
base[i];
}
set {
base[i] = value;
}
}
}
public
class Product {
public String Id;
public String Name;
public Product (String id, String name) {
this.Id = id;
this.Name = name;
}
}
public
class ProductList : ArrayList {
public
new Product
this[
int i] {
get {
return (Product)
base[i];
}
set {
base[i] = value;
}
}
}