Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have one problem,I have used one ASP.Net control Treeview that bind on run time.
I am using datatable to hold value from database afterthat using for loop to bind treeview,
But it comes in expend mode and when i have used colleps() method then it come in collsaps method but when i have select any page then treeview again goes in collaps mode.
Posted
Comments
johannesnestler 28-Jun-13 7:04am    
so you are re-binding the treeview after selecting another page? For that you may clear the treeview and rebuild it - so selection is lost and expanded/collapsed states too. I think you will have to re-expand the previouse selected node (store it and then call EnsureVisible function on the desired node - it will be expanded and scrolled into view if needed). If this is not what you meant - please clarify and/or show your code...
maneeshsingh07 28-Jun-13 7:14am    
hi johannesnestler

below is my sample code...

<div id="tabs" style="overflow:auto; width:250px; height:100%;">
<asp:TreeView ID="tr1" runat="server">

</div>


public void BindTree()
{
dt = lng.Getlogin_Parent(Session["UserID"].ToString());
for (i = 0; i < dt.Rows.Count; i++)
{
TreeNode no = new TreeNode();
no.Collapse();
no.Text = dt.Rows[i]["MENU_DESC"].ToString();
menuid = Convert.ToInt32(dt.Rows[i]["MENU_ID"]);
if (dt.Rows[i]["MENU_DESC"].ToString() == "Password Change")
{
no.NavigateUrl = "~/PasswordChange.aspx";
}
Get_allNodes(no);
this.tr1.Nodes.Add(no);

}
}
public void Get_allNodes(TreeNode no)
{
dt1 = lng.Getlogin_ParentID(Session["UserID"].ToString(), menuid);
{
for (j = 0; j < dt1.Rows.Count; j++)
{
TreeNode Child = new TreeNode();
Child.Collapse();
Child.Text = dt1.Rows[j]["MENU_DESC"].ToString();
menuchild = Convert.ToInt32(dt1.Rows[j]["MENU_ID"]);
if (dt1.Rows[j]["MENU_DESC"].ToString() == "Some Page")
{
Child.NavigateUrl = "~/somepage.aspx";
}
//Get_allNodesSubchild(Child);
no.ChildNodes.Add(Child);
}

}
}
johannesnestler 28-Jun-13 7:34am    
Hi maneeshsingh07,

So you call BindTree (which calls a "ladder" of Get_allNodesSubXXXChild (could be done with recursion or looping - but thats another topic)) after "select any page". BindTree always creates new nodes (with explicit Collapsed state). More relevant for your problem could be the code where you clear the treeview or the code that calls BindTree... But it seems my initial guess was right, don't you think so?

After that (as long time CP user I feel obliged to) I give you another tip - use reply button - so I get informed about your answer to my comment and use ImproveQuestion to add your code (then of course also formatted as code). <- Thank you.

1 solution

Code:
C#
 if (treeView1.SelectedNode.Index >= 0)
                {
                   
                        string qry = "";

                        TreeNode n2 = null;


                        System.Data.OleDb.OleDbDataReader or = null;
                        if (treeView1.SelectedNode.Level == 0)
                        {

                            qry = "select distinct column_name from tbl1;";
                            or = connect.m_rdr(qry);    \\binding with datareader
                            try
                            {
                                treeView1.SelectedNode.Nodes.Clear();
                                while (or.Read())
                                {
                                    n2 = new TreeNode(or.GetValue(0).ToString());
                                    treeView1.SelectedNode.Nodes.Add(n2);

                                }

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                            finally
                            {
                                or.Dispose();
                                or.Close();
                            }
                        }
}




It may prove to be helpful, although not sure about this code as I wrote it for windows forms.

See if it helps.
 
Share this answer
 

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