Set selected node in Treeview control in ASP:NET 2.0 ? Anyone know this ?

Discussion in 'ASP .Net' started by jesper_lofgren@yahoo.se, Dec 26, 2005.

  1. Guest

    Hello,

    Iam using Treeview control in asp.net 2.0. But have a problem.

    I use NavigateUrl BUT then viewstate is lost when clicked on a link i
    the menu. And the selected node is lost.

    Then i have to set the selected node manually, how can i do it ? i have
    tried but without luck :(
    I have posted my menu code here.


    namespace DayDream.Admin.UC
    {
    public partial class AdminMenu : DayDreamBaseUserControl
    {
    public string SelectedURL;

    protected void Page_Load(object sender, EventArgs e)
    {
    DataBind();


    }

    protected void PopulateNode(Object sender, TreeNodeEventArgs e)
    {

    // Call the appropriate method to populate a node at a
    particular level.
    switch (e.Node.Depth)
    {
    case 0:
    // Populate the first-level nodes.
    PopulateTopLevelNodes(e.Node);
    break;
    case 1:
    // Populate the second-level nodes.
    PopulateChildern(e.Node);
    break;
    default:
    // Do nothing.
    break;
    }

    }


    private void PopulateTopLevelNodes(TreeNode node)
    {
    DayDreamBasePage basePage = new DayDreamBasePage();

    for (int x = 0; x <
    basePage.GetAllPages().Tables[0].Rows.Count; x++)
    {
    if
    (basePage.GetAllPages().Tables[0].Rows[x]["ParentPageID"].ToString().Length
    <= 0)
    {

    TreeNode TopLevel = new TreeNode();

    //TopLevel.NavigateUrl =
    basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString() +
    "?id=" + basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
    TopLevel.NavigateUrl =
    "../../"+basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString()
    + "?id=" +
    basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
    TopLevel.Text =
    basePage.GetAllPages().Tables[0].Rows[x]["PageName"].ToString();
    TopLevel.Value =
    basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();

    // Add root node to TreeView
    TopLevel.PopulateOnDemand = true;
    TopLevel.SelectAction =
    TreeNodeSelectAction.SelectExpand;
    node.ChildNodes.Add(TopLevel);


    }

    }

    }


    private void PopulateChildern(TreeNode node)
    {

    DayDreamBasePage basePage = new DayDreamBasePage();

    DataSet Childrens = new DataSet();
    Childrens =
    basePage.GetChildrenPages(Int32.Parse(node.Value));

    for (int y = 0; y < Childrens.Tables[0].Rows.Count; y++)
    {

    TreeNode newNode = new TreeNode();
    newNode.NavigateUrl = "../../" +
    Childrens.Tables[0].Rows[y]["Template"].ToString() + "?id=" +
    Childrens.Tables[0].Rows[y]["PageID"].ToString();
    newNode.Text =
    Childrens.Tables[0].Rows[y]["PageName"].ToString();
    newNode.Value =
    Childrens.Tables[0].Rows[y]["PageID"].ToString();

    // Set additional properties for the node.
    newNode.SelectAction =
    TreeNodeSelectAction.SelectExpand;

    // Add the new node to the ChildNodes collection of the
    parent node.
    node.ChildNodes.Add(newNode);

    DataSet ChildrensSub = new DataSet();
    int ChildrensSubCount =
    basePage.GetChildrenPages(Int32.Parse(newNode.Value)).Tables[0].Rows.Count;

    if (ChildrensSubCount > 0)
    {
    PopulateChildern(newNode);
    }

    }
    }


    }
    }


    Thanks
    Jeppe
    , Dec 26, 2005
    #1
    1. Advertising

  2. RE: Set selected node in Treeview control in ASP:NET 2.0 ? Anyone know

    On Page_Load event you are not checking if Page isPostBack

    If Not (Page.IsPostBack) {
    DataBind();
    }

    Sandeep

    "" wrote:

    > Hello,
    >
    > Iam using Treeview control in asp.net 2.0. But have a problem.
    >
    > I use NavigateUrl BUT then viewstate is lost when clicked on a link i
    > the menu. And the selected node is lost.
    >
    > Then i have to set the selected node manually, how can i do it ? i have
    > tried but without luck :(
    > I have posted my menu code here.
    >
    >
    > namespace DayDream.Admin.UC
    > {
    > public partial class AdminMenu : DayDreamBaseUserControl
    > {
    > public string SelectedURL;
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > DataBind();
    >
    >
    > }
    >
    > protected void PopulateNode(Object sender, TreeNodeEventArgs e)
    > {
    >
    > // Call the appropriate method to populate a node at a
    > particular level.
    > switch (e.Node.Depth)
    > {
    > case 0:
    > // Populate the first-level nodes.
    > PopulateTopLevelNodes(e.Node);
    > break;
    > case 1:
    > // Populate the second-level nodes.
    > PopulateChildern(e.Node);
    > break;
    > default:
    > // Do nothing.
    > break;
    > }
    >
    > }
    >
    >
    > private void PopulateTopLevelNodes(TreeNode node)
    > {
    > DayDreamBasePage basePage = new DayDreamBasePage();
    >
    > for (int x = 0; x <
    > basePage.GetAllPages().Tables[0].Rows.Count; x++)
    > {
    > if
    > (basePage.GetAllPages().Tables[0].Rows[x]["ParentPageID"].ToString().Length
    > <= 0)
    > {
    >
    > TreeNode TopLevel = new TreeNode();
    >
    > //TopLevel.NavigateUrl =
    > basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString() +
    > "?id=" + basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
    > TopLevel.NavigateUrl =
    > "../../"+basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString()
    > + "?id=" +
    > basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
    > TopLevel.Text =
    > basePage.GetAllPages().Tables[0].Rows[x]["PageName"].ToString();
    > TopLevel.Value =
    > basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
    >
    > // Add root node to TreeView
    > TopLevel.PopulateOnDemand = true;
    > TopLevel.SelectAction =
    > TreeNodeSelectAction.SelectExpand;
    > node.ChildNodes.Add(TopLevel);
    >
    >
    > }
    >
    > }
    >
    > }
    >
    >
    > private void PopulateChildern(TreeNode node)
    > {
    >
    > DayDreamBasePage basePage = new DayDreamBasePage();
    >
    > DataSet Childrens = new DataSet();
    > Childrens =
    > basePage.GetChildrenPages(Int32.Parse(node.Value));
    >
    > for (int y = 0; y < Childrens.Tables[0].Rows.Count; y++)
    > {
    >
    > TreeNode newNode = new TreeNode();
    > newNode.NavigateUrl = "../../" +
    > Childrens.Tables[0].Rows[y]["Template"].ToString() + "?id=" +
    > Childrens.Tables[0].Rows[y]["PageID"].ToString();
    > newNode.Text =
    > Childrens.Tables[0].Rows[y]["PageName"].ToString();
    > newNode.Value =
    > Childrens.Tables[0].Rows[y]["PageID"].ToString();
    >
    > // Set additional properties for the node.
    > newNode.SelectAction =
    > TreeNodeSelectAction.SelectExpand;
    >
    > // Add the new node to the ChildNodes collection of the
    > parent node.
    > node.ChildNodes.Add(newNode);
    >
    > DataSet ChildrensSub = new DataSet();
    > int ChildrensSubCount =
    > basePage.GetChildrenPages(Int32.Parse(newNode.Value)).Tables[0].Rows.Count;
    >
    > if (ChildrensSubCount > 0)
    > {
    > PopulateChildern(newNode);
    > }
    >
    > }
    > }
    >
    >
    > }
    > }
    >
    >
    > Thanks
    > Jeppe
    >
    >
    =?Utf-8?B?U2FuZGVlcA==?=, Dec 28, 2005
    #2
    1. Advertising

  3. ro0oraa

    Joined:
    Sep 2, 2012
    Messages:
    1
    try this

    TreeNode node = TreeTypes.FindNode(obj.CustomerTypeId.ToString());


    TreeTypes.Nodes[TreeTypes.Nodes.IndexOf(node)].Select();
    ro0oraa, Sep 2, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,447
  2. Replies:
    3
    Views:
    9,055
    Daniel TIZON
    Dec 17, 2005
  3. =?Utf-8?B?V2Vz?=
    Replies:
    2
    Views:
    18,821
    sankar
    Jul 3, 2006
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  5. JJ
    Replies:
    1
    Views:
    1,091
Loading...

Share This Page