Mega Code Archive

 
Categories / ASP.Net / Asp Control
 

Database tree

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DataBaseTree" %> <!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>Custom TreeView</title> </head> <body>    <form id="form1" runat="server">    <div id="container">       <h1>Database TreeView </h1>       <p>This example fills a <code>TreeView</code> control from a database</p>       <h2>Tree Filled From Database</h2>             <asp:TreeView ID="treeMain"                      runat="server"                      CssClass="tree"                      ShowCheckBoxes="Leaf">       </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 DataBaseTree : 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);                string[] products = ProductCatalog.GetProductsBySubHeading(sub);                foreach (string prod in products)                {                   TreeNode productNode = new TreeNode(prod);                   subheadingNode.ChildNodes.Add(productNode);                }                groupNode.ChildNodes.Add(subheadingNode);             }             treeMain.Nodes.Add(groupNode);          }       }    } } 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;    } } 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;    } }