Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have got some issues with tree view.
My Tree View looks like this
MSIL
*****************************************

·         ABC

o   123

o   345

·         DEF

o   456

o   567

·         TYU

o   123

o   789

*****************************************

Questions:

1. When I am clicking on 123 child of TYU, the selected node parent is ABC in application. May I know why it is behaving in this way?

2. When I am selecting DEF or TYU, selection is moving to ABC itself. Any idea why this is happening.

Code:
For Populating tree:
C#
TreeNode lObjTreeNode = null;

string[] lArrTemp = null;
string lStrTemp = "";
trvRegionEnvMain.Nodes.Clear();
lStrTemp = Session[lObjClsGlobals.mStrViewStateRegionsList].ToString().Trim();
lArrTemp = lStrTemp.Split(',');
lObjTreeNode = new TreeNode("Regions And Environments");
lObjTreeNode.Value = lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_ROOT;
trvRegionEnvMain.Nodes.Add(lObjTreeNode);
foreach (string lStrItem in lArrTemp)
{
  lObjTreeNode = null;
  lObjTreeNode = new TreeNode(lStrItem.Trim());
  lObjTreeNode.SelectAction = treeNodeSelectAction.Select;
  lObjTreeNode.Value = lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_REGION;
  trvRegionEnvMain.Nodes[0].ChildNodes.Add(lObjTreeNode);
  lFnFillChildNodes(lObjTreeNode);
}

public void lFnFillChildNodes(TreeNode aObjTNode)
{
  try
  {
    ArrayList lObjArrLst = null;
    string[] lArrStrEnv = null;
    string lStrRegion = "";
    string[] lArrTemp = null;
    if (aObjTNode == null)
    {
      return;
    }
    lArrTemp = null;
    lObjArrLst = Session[lObjClsGlobals.mStrViewStateAllRegionsWithEnvList] as ArrayList;

    foreach (TreeNode lObjTN in trvRegionEnvMain.Nodes[0].ChildNodes)
    {
      if (lObjTN.Text == aObjTNode.Text)
      {
        lObjTN.Selected = true;
      }
    }
    string lStrCurrentNodeRegion = "";
    lStrCurrentNodeRegion = trvRegionEnvMain.SelectedNode.Text.Trim();
    foreach (string lStrItem in lObjArrLst)
    {
      if (lStrItem.Trim() == "")
      { continue; }
      lArrTemp = lStrItem.Split(',');
      lStrRegion = lArrTemp[0];
      if (lStrRegion.Trim() == lStrCurrentNodeRegion.Trim())
      {
        //trvRegionEnvMain.Nodes[0].ChildNodes[lIntNodeCnt].Selected = true;
        lArrStrEnv = lArrTemp[1].Trim().Split(':');
        foreach (string lStrEnv in lArrStrEnv)
        {
          string[] lArrActEnvReg = lStrEnv.Trim().Split('~');
          TreeNode lObjTreeNode = null;
          lObjTreeNode = new TreeNode(lArrActEnvReg[0].Trim());
          lObjTreeNode.SelectAction = TreeNodeSelectAction.Select;
          lObjTreeNode.Value = lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_ENV + "~" + lArrActEnvReg[1].Trim();
          trvRegionEnvMain.SelectedNode.ChildNodes.Add(lObjTreeNode);
        }
      }
    }
  }
  catch (Exception)
  {
    throw;
  }
}


UPDATE from OP:
Here is Treenode select change code..

C#
protected void trvRegionEnvMain_SelectedNodeChanged(object sender, EventArgs e)
      {
          try
          {
              string lStrTemp = "";
              if (trvRegionEnvMain.SelectedNode == null)
              {
                  return;
              }
              if (trvRegionEnvMain.SelectedNode.Value.Trim() == lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_ROOT)
              {
                  return;
              }

              if (trvRegionEnvMain.SelectedNode.Value.Trim() == lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_REGION)
              {

                  Session[lObjClsGlobals.mStrViewStateRegion] = trvRegionEnvMain.SelectedNode.Text.Trim();
                  Session[lObjClsGlobals.mStrViewStateOfActualEnvironment] = "";
                  if (trvRegionEnvMain.SelectedNode.ChildNodes.Count > 0)
                  {
                      Session[lObjClsGlobals.mStrViewStateOfActualEnvironment] = trvRegionEnvMain.SelectedNode.ChildNodes[0].Text.Trim();
                      lStrTemp = trvRegionEnvMain.SelectedNode.ChildNodes[0].Value.Trim();
                      if (lStrTemp.Split('~') != null)
                      {
                          Session[lObjClsGlobals.mStrViewStateForEnvandRegSplitDetails] = lStrTemp.Split('~')[1].Trim();
                      }
                  }

                  return;
              }
              string lStrTempEnv = "";
              if (trvRegionEnvMain.SelectedNode.Value.ToString().Split('~') != null)
              {
                  lStrTempEnv = trvRegionEnvMain.SelectedNode.Value.ToString().Split('~')[0];
              }
              if (lStrTempEnv.Trim() == lObjClsGlobals.CONST_STR_MAIN_TREENODE_TYPE_ENV)
              {
                                      Session[lObjClsGlobals.mStrViewStateOfActualEnvironment] = trvRegionEnvMain.SelectedNode.Text.Trim();
                  lStrTemp = trvRegionEnvMain.SelectedNode.Value.Trim();
                  if (lStrTemp.Split('~') != null)
                  {
                      Session[lObjClsGlobals.mStrViewStateForEnvandRegSplitDetails] = lStrTemp.Split('~')[1].Trim();
                  }
                  if (trvRegionEnvMain.SelectedNode.Parent != null)
                  {
                      Session[lObjClsGlobals.mStrViewStateRegion] = trvRegionEnvMain.SelectedNode.Parent.Text.Trim();
                  }

                  return;
              }
          }
          catch (Exception)
          {
              throw;
          }
      }
Posted
Updated 31-Jan-11 4:02am
v3
Comments
shreekar 31-Jan-11 7:45am    
As you yourself note, this behaviour happens when you click on the nodes. Please show the code you have written for that event.
Venkatesh Mookkan 1-Feb-11 0:24am    
Post your Page Loaded event. Let me see whether you have done it properly

Hi Venkat,

Thanks for reply. But I am populating tree in the same way,
 
Share this answer
 
Comments
Venkatesh Mookkan 1-Feb-11 0:23am    
Just add as a comment. Please do not post your replies as Answer. I will not get notified if you post as Answer.
Are you loading the TreeView on the Page Loaded event? If yes, check for IsPostback before calling the load.

I see like you are loading the TreeView on the Page Loaded without any condition check. So, the TreeView is regenerated with items everytime it is loaded.

Your code supposed to be,

C#
protected void Page_Loaded (object sender, EventArgs e)
{
    if (!IsPostback)
    {
        //Your loading logic should be here.
    }
}
 
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