Click here to Skip to main content
13,002,000 members (75,662 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 9 Jul 2009

User Controlled XML Menu For Site Navigation

, 9 Jul 2009
Rate this:
Please Sign up or sign in to vote.
A fully customizable ASP.NET website menu that can be set by site admin to control user access.



First of all, a huge thanks for giving a good response to my first article on CodeProject. In this article, I would illustrate the use of bit operations on integers to help us make a cool customizable menu. As always, my motto is minimum programming effort.


You need to have a good understanding of the .NET CLR to better understand this article, as the concept relies heavily on it.

Let me first of all brief you on the use of the bitwise '&' operator. This operator is used to check the status of bits in integer values. Note the following about '&':

  1. 0 & 1 = 0
  2. 1 & 0 = 0
  3. 0 & 0 = 0
  4. 1 & 1 = 1

I would not go into the details of converting an integer to bits. But just note the following property:

10111011 &  00100000 = 00100000

I.e., 187 & 32 = 32

Using the Code

We would need to add SQL CLR to our code, as applying '&' to an integer is illegal in C#. We do this by using the Microsoft.SqlServer.Server namespace. For building up this menu, we assign a score in the form of 2^n to each item as:

<?xml version="1.0"?>
    <MenuItem Title="A" URL="Page1.aspx" Value="1">
        <MenuItem Title="A.1" URL="Page1-1.aspx" Value="2" />
        <MenuItem Title="A.2" URL="Page1-2.aspx" Value="4" />

    <MenuItem Title="B" URL="Page2.aspx" Value="8">
        <MenuItem Title="B.1" URL="Page2-1.aspx" Value="16"/>
        <MenuItem Title="B.2" URL="Page2-2.aspx" Value="32"/>

    <MenuItem Title="C" URL="Page3.aspx" Value="64">
        <MenuItem Title="C.1" URL="Page3-1.aspx" Value="128"/>
        <MenuItem Title="C.2" URL="Page3-2.aspx" Value="256">
            <MenuItem Title="C.2.1" URL="Page3-2-1.aspx" Value="512"/>
            <MenuItem Title="C.2.2" URL="Page3-2-2.aspx" Value="1024">
                <MenuItem Title="C.2.2.1" URL="Page3-2-2.aspx" Value="2048"/>

We also assign a score to each user that is calculated as the sum of all the values of menu items that we want the user to see. Say, we want a user to only see the Main menu A with all the subitems, then the score will be 1+2+4 = 7. Note that you need to add up the main menu score, i.e., the score of A, B, or C if you want to use any of its child menus.

Now, let's jump to the last critical part, that is the CLR code that performs the '&' operation with the menu's values to see whether a user is eligible to see the navigation link or not.

private bool Addable(int score, int valueToCheck)
    return ((score & valueToCheck) == valueToCheck);

Now, all we need is a record of score of each user and get the required XML data source to bind to a menu or tree. Each menu item's value is checked for its '&' ability with the total score, to see if it fits the user's menu criteria.

if (score != 0)
    datasetReadXML = new DataSet();
    datasetMenuItems = new DataSet();
    xdsMenuData = new XmlDataSource();

    ///Necessary for Displaying Updated Values.
    xdsMenuData.EnableCaching = false;
    xdsMenuData.EnableViewState = false;

    ///Read XML and prepare a menu data holder.
    datasetMenuItems = datasetReadXML.Clone();

    ///Get required menu items in the holder.
    foreach (DataRow drWorker in datasetReadXML.Tables[0].Rows)
        if (Addable(score, Convert.ToInt32(drWorker["Value"])))

    ///Make a StringWriter to read XML.
    stringWriter = new System.IO.StringWriter();
    datasetMenuItems.WriteXml(stringWriter, XmlWriteMode.IgnoreSchema);

    ///Populate XMLDataSource with XML.
    xdsMenuData.Data = stringWriter.ToString();

    ///Bind menu or tree.
    TreeMenu.DataSource = xdsMenuData;
    Menu.DataSource = xdsMenuData;


I have used XML to keep track of user scores. However, you may use your own choice of database to do the same. All you need is a score. The attached code is self explanatory.

Points of Interest

Please remember to set the EnableCaching and EnableViewState properties of the XML data source to false for getting the updated values. I have seen a lot of people on various forums going nuts over it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author
Software Developer HCL Technologies Limited
India India
Rahul has worked on several technologies in Microsoft's technology stack. He has been in field of software developemnt for two years. He started with and graduated to Windows application develeopment. His technology experience includes C#, LINQ, T-SQL,T-SQL, XML, PL\SQL, C, C++, VB, WebServices..

Apart from all these he is working on some of the most recent technology offerings such as Windows Azure, SQL Azure, SQL Azure Sync framework, MS Dallas, Powershell etc.

His goal of knowledge is to possess knowledge that is sky high and earth deep.

You may also be interested in...


Comments and Discussions

MeraMBA.com9-Jul-09 19:49
memberMeraMBA.com9-Jul-09 19:49 
MeraMBA.com11-Jul-09 5:07
memberMeraMBA.com11-Jul-09 5:07 
Generalvery nice... one thing Pin
ChowDawg9-Jul-09 10:48
memberChowDawg9-Jul-09 10:48 
GeneralRe: very nice... one thing Pin
MeraMBA.com9-Jul-09 19:45
memberMeraMBA.com9-Jul-09 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 9 Jul 2009
Article Copyright 2009 by
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid