Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Friday, September 19, 2008

TreeView Example In Asp.net 2.0


Write Down Following method in TreeNodePopulateEvent Of TreeView
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {

        PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
    
    }


Write Down Foloowing Code in Page Load.
 protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            PopulateRootLevel();
          
        }
    }

For Populate Root Node
protected void PopulateRootLevel()
{
    con.Open();
    SqlCommand command = new SqlCommand(Query For Populate Root Node, con);
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataSet RootTable = new DataSet();
    adapter.Fill(RootTable);
    PopulateNodes(RootTable.Tables[0], TreeView1.Nodes);
    con.Close();

}

Ser Text And Value For Each Parent Node.

    protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        
        foreach(DataRow row in dt.Rows)
        {
            TreeNode tn=new TreeNode();
            tn.Text = "Text For Node"
            tn.Value = Value For Node ex. row["vacany_code"].ToString();
            nodes.Add(tn);
            tn.PopulateOnDemand = true;
          
        }
        
    }

Populate Child Node and Set Text And Value Field For Chid Nodes.

    protected void PopulateSubLevel(Int64 parent_id, TreeNode parent_node)
    {
        if (parent_id != 0)
        {
            con.Open();
            SqlCommand command = new SqlCommand(Query For Child, con);
            command.Parameters.Add("@employer_id", SqlDbType.Int).Value =
            parent_id;
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ChildDataSet = new DataSet();
            adapter.Fill(ChildTable);
            TreeNodeCollection nodes = parent_node.ChildNodes;
            foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
            {
                TreeNode tn = new TreeNode();
               
                tn.Text = "Text For Child Node
                tn.Value = "Value For Chid Node";
                nodes.Add(tn);
                tn.PopulateOnDemand = false;
            }
            con.Close();
          }
    }


Enjoy !!!!!!!!!!!!!!!!!!




Prev Post                                                      All Post                                                           Next Post



2 comments: