13,002,000 members (75,662 online)
alternative version

#### Stats

15.6K views
13 bookmarked
Posted 9 Jul 2009

, 9 Jul 2009
 Rate this:
A fully customizable ASP.NET website menu that can be set by site admin to control user access.

## Introduction

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.

## Background

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"?>

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.

```[Microsoft.SqlServer.Server.SqlFunction()]
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)
{

///Necessary for Displaying Updated Values.

///Get required menu items in the holder.

///Make a StringWriter to read XML.
stringWriter = new System.IO.StringWriter();

///Populate XMLDataSource with XML.

this.DataBind();
}```

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.

## Share

 Software Developer HCL Technologies Limited 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 ASP.net 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...

 Pro

 First Prev Next
 ATTENTION PLEASE MeraMBA.com9-Jul-09 19:49 MeraMBA.com 9-Jul-09 19:49
 Re: ATTENTION PLEASE MeraMBA.com11-Jul-09 5:07 MeraMBA.com 11-Jul-09 5:07
 very nice... one thing ChowDawg9-Jul-09 10:48 ChowDawg 9-Jul-09 10:48
 Re: very nice... one thing MeraMBA.com9-Jul-09 19:45 MeraMBA.com 9-Jul-09 19:45
 Last Visit: 31-Dec-99 18:00     Last Update: 26-Jun-17 10:14 Refresh 1