Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
NOTE :TreeView1 is the id of treeview and in dt1 has 100 records which it gets from database.

C#
PopulateNodes(dt1, TreeView1.Nodes); 

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
        {
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();

                tn.Text = dr["columnfield"].ToString().Trim();

                tn.Value = dr["code_webcategory_id"].ToString().Trim();
                 tn.ImageUrl = string.Format("image.aspx?category={0}&width=50", tn.Value);
                nodes.Add(tn);

             
               tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);

	}


        }

The above coding works fine.

NOTE: image.aspx, image comes from database(which comes as byte) and then writting as image using System.Drawing.Image bitmap = System.Drawing.Image.FromStream(stream),......the image comes from database and binds in tn.imageurl correctly. but here once the populateNodes function gets over only, the image.aspx calls to get image accordiing to node.

so, right now as i have 100 records it binds image 100 time which is ok.

now the below coding to bind child notes
C#
private void PopulateSubLevel(int parentid, TreeNode parentNode)
 {

     TreeNode parent= parentNode;

     PopulateNodes(dt, parent.ChildNodes);


 }

this also works fine. but,

Now, consider the child node has 10 records. once the populateNodes function gets over , it calls image.aspx to get an image. Now it binds 110 time again instead of binding 10 times.

this is the problem. how to overcome? any solution? help needed.
Posted
Updated 18-Mar-14 3:30am
v2
Comments
Nisarg Desai 18-Mar-14 6:19am    
If You Know That is 10 Times Then Why don`t You Take For Each For Each Single Node Of Treeview1
christhuxavier 19-Mar-14 5:55am    
hi. i used for each only.. i posted full coding. you can see what is the problem

private void bindingroodrecord()

{

sql = "EXEC [sp_parent_record]"; //stored procedur to get parent records

SqlCommand cmd = new SqlCommand(sql, objConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
da.Fill(dt1);
Session["parentnode"] = dt1;

PopulateNodes(dt1, TreeView1.Nodes); ///////// for parent node

}

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();

tn.Text = dr["columnfield"].ToString().Trim();

tn.Value = dr["code_webcategory_id"].ToString().Trim();
tn.ImageUrl = string.Format("image.aspx?category={0}&width=50", tn.Value);
nodes.Add(tn);


tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);

}


}
The above coding works fine.

NOTE: image.aspx, image comes from database(which comes as byte) and then writting as image using System.Drawing.Image bitmap = System.Drawing.Image.FromStream(stream),......the image comes from database and binds in tn.imageurl correctly.

so, right now as i have 100 records it binds image 100 time which is ok.

now the below coding to bind child notes

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{


PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node); // i am getting parent node when user click (populate)on treeview to form a child node for parent node.


}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{

SqlCommand cmd = new SqlCommand("sp_getchildrecords", objConn);/// this is the stored procedure to get child node using parent id.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Session["childnode"] = dt;
Session["childnodess"] = parentNode;
TreeNode childrecords = (TreeNode)Session["childnodess"];

PopulateNodes(dt, childrecords.ChildNodes); // to form child nodes


}
this also works fine. but,

Now, consider the child node has 10 records. once the populateNodes function gets over , it calls image.aspx to get an image. Now it binds 110 time again instead of binding 10 times.
syed shanu 18-Mar-14 22:29pm    
I think you have parent node and sub node.So parent node and sub node has diffrent image links .I think you want to create 2 loop one for parent node and inner loop for the child node.
christhuxavier 19-Mar-14 7:06am    
Hi Syed, as you said. i worked on that. now it binds for child node only. but the issues the image shows only for child node. parent node is showing empty image.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900