if (!IsPostBack)
{
Populate(TreeView1.Nodes);
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = TreeView1.SelectedNode;
string nodeId = node.Value;
string nodeName = node.Text;
}
private void Populate(TreeNodeCollection nodes)
{
TreeNode parentNode = null;
foreach (Category1 category in GetProductCategories())
{
parentNode = new TreeNode(category.Name, category.Id.ToString());
if (category.Subcategories != null)
{
SubNodes(category, parentNode);
}
parentNode.Collapse();
TreeView1.Nodes.Add(parentNode);
}
}
private void SubNodes(Category1 category, TreeNode childNode)
{
foreach (Category1 c1 in category.Subcategories)
{
TreeNode subchildnode = new TreeNode(c1.Name, c1.Id.ToString());
childNode.ChildNodes.Add(subchildnode);
if (c1.Subcategories != null)
{
SubNodes(c1, subchildnode);
}
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
}
#region unwanted
// Show all checkboxes
//TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
//protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
//{
// PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
//}
//private void PopulateSubLevel(int parentid, TreeNode parentnode)
//{
// Populate1( parentid,parentnode.ChildNodes);
//}
//private void Populate1(int k1,TreeNodeCollection nodes)
//{
// foreach (var j in GetProductCategories())
// {
// if (j.ParentId == k1)
// {
// foreach (var k in j.Subcategories)
// {
// TreeNode tn = new TreeNode();
// tn.Text = k.Name;
// tn.Value = k.ParentId.ToString();
// nodes.Add(tn);
// //tn.PopulateOnDemand = ((int)(k.Subcategories.Count) > 0);
// }
// // break;
// }
// }
//}
#endregion
#region class
//public List<Category1> GetProductCategories()
//{
// return new List<Category1> {
// new Category1{Id=1,Name="Power Tools",ParentId=0,Sequence=1,Description="Power Tools",DisplayInHeader=true,
// Subcategories = new List<Category1>
// {new Category1{Id=100,Name="Drills",ParentId=1,Sequence=1,Description="Drills",DisplayInHeader=true},
// new Category1{Id=101,Name="Drill Accessories",ParentId=1,Sequence=1,Description="Drill Accessories",DisplayInHeader=true},
// new Category1{Id=102,Name="Saws",ParentId=0,Sequence=1,Description="Saws",DisplayInHeader=true}
// }},
// new Category1{Id=2,Name="Cordless Tools",ParentId=1,Sequence=1,Description="Cordless Tools",DisplayInHeader=true,
// Subcategories = new List<Category1>
// {new Category1{Id=200,Name="Batteries/Chargers",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true}
// }},new Category1{Id=3,Name="AirTools",ParentId=2,Sequence=1,Description="Air Tools",DisplayInHeader=true,
// Subcategories = new List<Category1>
// {new Category1{Id=300,Name="Portable Compressors",ParentId=0,Sequence=1,Description="Portable Compressors",DisplayInHeader=true},
// }}
// };
//}
#endregion
public List<Category1> GetProductCategories()
{
return new List<Category1> {
new Category1{Id=1,Name="Power Tools",ParentId=0,Sequence=1,Description="Power Tools",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=100,Name="Drills",ParentId=1,Sequence=1,Description="Drills",DisplayInHeader=true},
new Category1{Id=101,Name="Drill Accessories",ParentId=1,Sequence=1,Description="Drill Accessories",DisplayInHeader=true,
Subcategories= new List<Category1>{new Category1{Id=1001,Name="Drill Accessories",ParentId=101,Sequence=1,Description="Drill Accessories",DisplayInHeader=true,
Subcategories= new List<Category1>{new Category1{Id=1001,Name="Drill Accessories",ParentId=101,Sequence=1,Description="Drill Accessories",DisplayInHeader=true}}}}},
new Category1{Id=102,Name="Saws",ParentId=0,Sequence=1,Description="Saws",DisplayInHeader=true}
}},
new Category1{Id=2,Name="Cordless Tools",ParentId=1,Sequence=1,Description="Cordless Tools",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=200,Name="Batteries/Chargers",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true}}}}}}}}}
}},new Category1{Id=3,Name="AirTools",ParentId=2,Sequence=1,Description="Air Tools",DisplayInHeader=true,
Subcategories = new List<Category1>
{new Category1{Id=300,Name="Portable Compressors",ParentId=0,Sequence=1,Description="Portable Compressors",DisplayInHeader=true},
}},
};
}