Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:05:34+00:00 2026-06-05T07:05:34+00:00

In my below code , I have a list of AccessId, that is store

  • 0

In my below code , I have a list of AccessId, that is store in tag of nodes of tree.
How can I checked nodes (or childnodes) that tag of them is in my list ?

List<AccessFieldSet> AccessList = new List<AccessFieldSet>();
private void GetRolesAccessData(Int32 RolesId)
{            
    C_RolesUsers Db = new C_RolesUsers();
    AccessList = Db.GetRolesAccessData(RolesId);
    foreach (AccessFieldSet Afs in AccessList)
    {
        foreach (TreeNode node in TreeRoles.Nodes)
        {
            if (node.Tag == Afs.AccessId.ToString())
            {
                //Check Node is true ?
            }
            GetTagChildren(node);
        }
    }
}

private void GetTagChildren(TreeNode Node)
{
    TreeNode ChNode = null;
    //TreeFieldSet nodeCat = (TreeFieldSet)Node.Tag;
    //Int32 nodeCat = (Int32)Node.Tag;
    foreach (AccessFieldSet Afs in AccessList)
    {
        if (Afs.AccessId.ToString() == Node.Tag)
        {
            //Check Node is true ?
            GetTagChildren(ChNode);
        }
    }
}

I edit my code to :

List<AccessFieldSet> AccessList = new List<AccessFieldSet>();
private void GetRolesAccessData(Int32 RolesId)
{            
    C_RolesUsers Db = new C_RolesUsers();
    AccessList = Db.GetRolesAccessData(RolesId);
    for (int i = 0; i < TreeRoles.Nodes.Count; i++)
    {
        CheckedSelectedNodes(i, TreeRoles.Nodes, AccessList);
    }
}

private void CheckedSelectedNodes(Int32 i, TreeNodeCollection nodes, List<AccessFieldSet> AccessList)
{
    TreeNode node = nodes[i];
    for (int j = 0; j < AccessList.Count; j++)
    {
        foreach (AccessFieldSet Afs in AccessList)
        {
            if ((int)node.Tag == Afs.AccessId)
            {
                node.Checked = true;
            }
        }
    }
}

With new code, I can checked nodes that node’s tag is in AccessList. How can I change code for ChildNodes(Childs of nodes)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:05:35+00:00Added an answer on June 5, 2026 at 7:05 am

    You can use recursion to do this quite elegantly.

    List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 
    
    private void GetRolesAccessData(Int32 RolesId) 
    {             
        C_RolesUsers Db = new C_RolesUsers(); 
        accessList = Db.GetRolesAccessData(RolesId);
        foreach (TreeNode node in TreeRoles.Nodes)
        {
            CheckNodeRecursively(node, accessList);
        } 
    } 
    
    private void CheckNodeRecursively(TreeNode node, List<AccessFieldSet> accessList) 
    {
        // Note: You don't need the for loop through 'j'.
        foreach (AccessFieldSet afs in accessList) 
        { 
            if ((int)node.Tag == afs.AccessId) 
            { 
                node.Checked = true; 
            } 
        }
        foreach (TreeNode childNode in node.Nodes)
        {
            CheckNodeRecursively(childNode, accessList);
        }
    } 
    

    If you find yourself doing this sort of thing often, you can factor out the recursion into a utility method and use Action to just vary the “act-on-each-node” logic. For example:

    public static void ActOnAllRecursively(this TreeNodeCollection nodes, Action<TreeNode> action)
    {
        foreach (TreeNode node in nodes)
        {
            ActOnRecursively(node, action);
        }
    }
    
    public static void ActOnRecursively(this TreeNode node, Action<TreeNode> action)
    {
        action(node);
        foreach (TreeNode node in node.Nodes)
        {
            ActOnRecursively(node);
        }
    }
    

    Then your code just looks like this:

    List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 
    
    private void GetRolesAccessData(Int32 RolesId) 
    {             
        C_RolesUsers Db = new C_RolesUsers(); 
        accessList = Db.GetRolesAccessData(RolesId);
        TreeRoles.Nodes.ActOnAllRecursively((node) =>
        {
            foreach (AccessFieldSet afs in accessList) 
            { 
                if ((int)node.Tag == afs.AccessId) 
                { 
                    node.Checked = true; 
                } 
            } 
        });
    }
    

    And finally, you could go crazy with LINQ and write your app-specific code in a declarative style:

    List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 
    
    private void GetRolesAccessData(Int32 RolesId) 
    {             
        C_RolesUsers Db = new C_RolesUsers(); 
        accessList = Db.GetRolesAccessData(RolesId);
        TreeRoles.Nodes.ActOnAllRecursively((node) =>
        {
            node.Checked = accessList.Any(afs => afs.AccessId == (int)node.Tag);
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Referencing the code below, I have a list of Error objects that has been
I have the code below for getting a list of child processes on windows
I have the below code, which iterates over a list based on a custom
I need your opinion about this code below. I have a list of messages:
I have a long list of links that I spit out using the below
In the code behind I have a function that returns a List(Of SomeClass): rptRepeater.DataSource
Form contains two dropdown lists created using code below. Both dropdown list elements have
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
In the code below, I have a List object. I want to iterate through
I have python code below that will loop through a table and print out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.