I had seen many people facing the problem in recursive function building so they try and avoid recursive function but recursive function’s are really help full in many places at the time of programming in looping through a unknown length of tables and fast too although try and use recursive function more carefully other wise they could also slow down the process many a time (if condition are not applied properly recursive function can fall in end less looping). We can avoid recursive function but some time recursive function are required and unavoidable (e.g. an building a tree function for ASP.NET Tree control) I have given a code for simple example in ASP.NET of Tree control as I needed it myself so I build itJ.
I had taken back end (Database) as MS SQL 2000 created a table called tree having Following Structure:
1. ID (Primary Key)
2. ItemName
3. ParentID
4. Active
You can add more fields according to your needs
ParentID will be 0 for root Node
e.g. like 'TV' is at parent and 'LG TV' an 'Samsung TV' are as child of 'TV' Data will be like.
----------------------------------
ID | ItemName | ParentID | Active
----------------------------------
23 | TV | 0 | 1
24 | Samsung TV | 23 | 1
25 | LG TV | 23 | 1
26 | Wash Machine | 0 | 1
27 | IFB WM | 26 | 1
28 | Whirlpool | 26 | 1
This is the following code
Please include following namespaces at the top
using System.Data.SqlClient;
using System.IO;
//Connection string CON used from web.config file
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["CON"].ToString();
//Load first time only to avoid unnessary Recursion loops
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateTree();
}
}
//Called at first time to populate Root node values
private void PopulateTree()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from Tree where active=1";
cmd.Connection = con;
DataTable dtMenu = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtMenu);
DataView dv = new DataView(dtMenu);
dv.RowFilter = "ParentID=0";
int i;
TreeView_Control.Nodes.Clear();
for (i = 0; i <>
{
TreeNode node = new TreeNode();
node.Value = dv[i]["ID"].ToString();
node.Text = dv[i]["ItemName"].ToString();
TreeView_Control.Nodes.Add(node);
RecFillTree(dtMenu, node);
}
}
//recursive function Called by PopulateTree() to populate Child of //nodes will go to any u nbroken depth and start by next ParentNode
private void RecFillTree(DataTable dtMenu, TreeNode ParentNode)
{
DataView dv = new DataView(dtMenu);
dv.RowFilter = "ParentID =" + ParentNode.Value;
int i;
if (dv.Count > 0)
{
for (i = 0; i <>
{
TreeNode node = new TreeNode();
node.Value = dv[i]["ID"].ToString();
node.Text = dv[i]["ItemName"].ToString();
ParentNode.ChildNodes.Add(node);
RecFillTree(dtMenu, node);
}
}
}
//displaying the value and text of the select node
protected void TreeView_Control_SelectedNodeChanged(object sender, EventArgs e)
{
LblTreeNode_Text.Text=TreeView_Control.SelectedNode.Text;
LblTreeNode_Value.Text = TreeView_Control.SelectedNode.Value;
}
}
I think this articales will help in your various Recurcive Looping and Tree control building problem.