February 20, 2019
Hot Topics:

Implement UI Role Awareness in Your Applications

  • February 2, 2005
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

Recursion: The Natural Approach

The Controls collection, C# "is" statement, and a common interface are clearly the tools for the job. However, you must circumvent one other complication before you have a complete solution: A Web Form can contain a number of user controls, and user controls can themselves contain layers of user controls and other server controls. How do you navigate all the user-control layers within a Web Form?

Invoking a function at the upper-most level and allowing the process to cascade downward to child, child-of-child, child-of-child-of-child, and so forth is a good approach. A straightforward, although not quite as good, way to execute the role-changes functions is to invoke the role-change function in the "Load" event of each Web Form or user control.

However, the most natural choice for navigating hierarchical or Binary-tree-like data structures like the layers of user controls on a Web Form is the recursive function. In the following example, the Web Form becomes the root node and the user controls on the form, along with the user controls on the Web Form's user controls, become the leaves in the tree:

public void SetupControl ( ListBox lb,Control c,int level )
   PageSecurityRoleSetup pv;

   pv = null;

   ListBox1 = lb;

   VisitTheControl (level, c);    //Visit the parent

   foreach (Control cNext in c.Controls)
      pv = new PageSecurityRoleSetup ();

      pv.SetupControl (lb, cNext,level+1);    //Recurse to next set of

      VisitTheControl (level, cNext);

The PageSecurityRoleSetup object creates an instance of another PageSecurityRoleSetup object when the SetupControl function is invoked. Once the controls at the deepest levels are visited, the recursive function terminates and the recursive process unwinds. The InvokeRoleSetup function calls the proper state-role function on the interface using a switch statement. The proper case statement is activated based on an enumerated value.

Implement Role Changes with a Single Switch Statement

Now you know an elegant and flexible solution for solving the role-change problem often encountered in Web sites with multiple user roles. Implementing role changes need not require scores of switch statements. By using the Controls collection and recursion, you can implement role changes with a single switch statement.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

Jeffrey Juday is a software developer with Crowe Chizek in South Bend, Indiana. He has been developing software with Microsoft tools for more than 12 years in a variety of industries. Jeff currently builds solutions using BizTalk 2004, ASP.NET, Sharepoint, and SQL Server 2000. You can reach Jeff at jjuday@crowechizek.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date