protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateRootLevel();
}
}
public void PopulateRootLevel()
{
DataSet PrSet = PDataset("Select * from [Tbl_MasterCategory]");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["Cat_Name"].ToString();
tnParent.Value = dr["Cat_Id"].ToString();
tnParent.PopulateOnDemand = true;
tnParent.ToolTip = "Click to get Child";
tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
tnParent.Selected = true;
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
}
}
public void FillChild(TreeNode parent, string ParentId)
{
DataSet ds = PDataset("Select * from [Tbl_SubCategory] where Cat_Id =" + ParentId);
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode subparent = new TreeNode(dr["Scat_Name"].ToString(),Convert.ToString(dr["Cat_Id"]));
parent.ChildNodes.Add(subparent);
subparent.Value = Convert.ToString(dr["SCat_Id"]);
//TreeNode child = new TreeNode();
//child.Text = dr["Scat_Name"].ToString().Trim();
//child.Value = dr["SCat_Id"].ToString().Trim();
//if (child.ChildNodes.Count == 0)
//{
// child.PopulateOnDemand = true;
//}
//child.ToolTip = "Click to get Child";
//child.SelectAction = TreeNodeSelectAction.SelectExpand;
//child.CollapseAll();
//parent.ChildNodes.Add(child);
FillsubChild(subparent, subparent.Value);
}
}
public void FillsubChild(TreeNode childnode, string childId)
{
DataSet ds = PDataset("Select * from Tbl_ChildCategory where SCat_Id =" + childId);
//childnode.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode subchild = new TreeNode(dr["Ccat_Name"].ToString(), Convert.ToString(dr["SCat_Id"]));
childnode.ChildNodes.Add(subchild);
subchild.Value = Convert.ToString(dr["CCat_Id"]);
//TreeNode subchild = new TreeNode();
//subchild.Text = dr[2].ToString().Trim();
//subchild.Value = dr[1].ToString().Trim();
//if (subchild.ChildNodes.Count == 0)
//{
// subchild.PopulateOnDemand = true;
//}
//subchild.ToolTip = "Click to get Child";
//subchild.SelectAction = TreeNodeSelectAction.SelectExpand;
//subchild.CollapseAll();
//subchild.ChildNodes.Add(childnode);
}
}
protected DataSet PDataset(string Select_Statement)
{
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, sqlconn);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
{
if (!IsPostBack)
{
PopulateRootLevel();
}
}
public void PopulateRootLevel()
{
DataSet PrSet = PDataset("Select * from [Tbl_MasterCategory]");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["Cat_Name"].ToString();
tnParent.Value = dr["Cat_Id"].ToString();
tnParent.PopulateOnDemand = true;
tnParent.ToolTip = "Click to get Child";
tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
tnParent.Selected = true;
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
}
}
public void FillChild(TreeNode parent, string ParentId)
{
DataSet ds = PDataset("Select * from [Tbl_SubCategory] where Cat_Id =" + ParentId);
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode subparent = new TreeNode(dr["Scat_Name"].ToString(),Convert.ToString(dr["Cat_Id"]));
parent.ChildNodes.Add(subparent);
subparent.Value = Convert.ToString(dr["SCat_Id"]);
//TreeNode child = new TreeNode();
//child.Text = dr["Scat_Name"].ToString().Trim();
//child.Value = dr["SCat_Id"].ToString().Trim();
//if (child.ChildNodes.Count == 0)
//{
// child.PopulateOnDemand = true;
//}
//child.ToolTip = "Click to get Child";
//child.SelectAction = TreeNodeSelectAction.SelectExpand;
//child.CollapseAll();
//parent.ChildNodes.Add(child);
FillsubChild(subparent, subparent.Value);
}
}
public void FillsubChild(TreeNode childnode, string childId)
{
DataSet ds = PDataset("Select * from Tbl_ChildCategory where SCat_Id =" + childId);
//childnode.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode subchild = new TreeNode(dr["Ccat_Name"].ToString(), Convert.ToString(dr["SCat_Id"]));
childnode.ChildNodes.Add(subchild);
subchild.Value = Convert.ToString(dr["CCat_Id"]);
//TreeNode subchild = new TreeNode();
//subchild.Text = dr[2].ToString().Trim();
//subchild.Value = dr[1].ToString().Trim();
//if (subchild.ChildNodes.Count == 0)
//{
// subchild.PopulateOnDemand = true;
//}
//subchild.ToolTip = "Click to get Child";
//subchild.SelectAction = TreeNodeSelectAction.SelectExpand;
//subchild.CollapseAll();
//subchild.ChildNodes.Add(childnode);
}
}
protected DataSet PDataset(string Select_Statement)
{
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, sqlconn);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}