Click here to Skip to main content
Licence CPOL
First Posted 22 May 2009
Views 16,376
Downloads 359
Bookmarked 20 times

Binding Sitemaps with Menu Control Based on User Privilege at Runtime

By | 22 May 2009 | Article
Binding sitemaps with menu control based on user privilege at runtime

Introduction

This article will help you to bind Menu Control (Under Navigation tab) with different siteMaps according to user privileges. For example, if user privilege is Administrator then it will show a different menu, if user privilege is normal user, it will show a different menu. It will bind menu control with siteMap at runtime according to user privileges.

Background

For navigating one page to another page in web sites, siteMap and Menu control play a big role. We can define page URL in siteMap and bind with menu control, after that we can easily navigate from one page to another page.

Using the Code

Here is an example to show how we can navigate from one page to another page according to user privileges.

Step 1

Create a page named as Home.aspx or you should create a Master Page and drag Menu control on Master page because whenever you will add a master page in any page, that menu control will be added automatically on that page. But here, I am giving you an idea about how we will create a siteMap and bind that siteMap with Menu control. Drag Menu control on your Home.aspx page, and right click on Menu1, select properties and change Orientation as vertical to Horizontal or whatever you want. Now create three pages named as CreateUser.aspx, Software.aspx, and Hardware.aspx.

Step 2

Now right click on your solution name and click on 'Add New Item' and choose siteMap named as Administrator.siteMap. Like this, take two more siteMaps named as User.siteMap and Default.siteMap. Now define all menu Items in siteMap according to user privilege.

For Administrator.siteMap

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode url="~/Home.aspx" title="Home" description="HomePage"/>
<siteMapNode url="~/CreateUser.aspx" title="CreateUser" description="CreateUser" />
<siteMapNode url="" title="Products" description="Products" >
<siteMapNode url="~/Software.aspx" title="Software" description="Software" />
<siteMapNode url="Hardware.aspx" title="Hardware" description="Hardware" />
</siteMapNode>
</siteMap>

For User.siteMap

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode url="~/Home.aspx" title="Home" description="HomePage"/> 
<siteMapNode url="" title="Products" description="Products" >
<siteMapNode url="~/Software.aspx" title="Software" description="Software" />
<siteMapNode url="Hardware.aspx" title="Hardware" description="Hardware" />
</siteMapNode>
</siteMap>

Now create a third siteMap Default.siteMap as user.siteMap that I already created above.

Step 3

Go to Home.aspx page, in source add DataBindings elements inside asp:menu element as described below:

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" 
	Width="383px" style="z-index: 100; left: 203px; position: absolute; 
	top: 57px" BackColor="#FFFBD6" Font-Bold="True" Font-Names="Verdana" 
	Font-Size="Medium" ForeColor="#990000" StaticSubMenuIndent="10px">
	<DataBindings>
<asp:MenuItemBinding DataMember="siteMapNode" 
	NavigateUrlField="url" TextField="title" /> 
</DataBindings>
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuStyle BackColor="#FFFBD6" />
<StaticSelectedStyle BackColor="#FFCC66" />
<DynamicSelectedStyle BackColor="#FFCC66" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticHoverStyle BackColor="#990000" ForeColor="White" />
</asp:Menu>

Step 4

Now go to Home.aspx.cs page and create one function called GetDataSource():

XmlDataSource GetDataSource(string UserRole, string ServerPath)
{
    XmlDataSource oXmlDataSource = new XmlDataSource();
    oXmlDataSource.XPath = "siteMap/siteMapNode";
    switch (UserRole)
    {
        case "Administrator":
        oXmlDataSource.DataFile = ServerPath + @"/App_Data/Administrator.sitemap";
        break;
        case "User":
        oXmlDataSource.DataFile = ServerPath + @"/App_Data/User.sitemap";
        break;
        default:
        oXmlDataSource.DataFile = ServerPath + @"/App_Data/Default.sitemap";
        break;
    }
    oXmlDataSource.DataBind();
    return oXmlDataSource;
}

Step 5

In Home.aspx.cs Page_Load assign DataSource to Menu1 and bind the Menu1.

protected void Page_Load(object sender, EventArgs e)
{
    Menu1.DataSource = GetDataSource(strUserRole, Server.MapPath("~"));
    Menu1.DataBind();
}

Now your Menu is binded with siteMaps.

Step 6

Note: For understanding purposes, I took two buttons named as AdminRole and UserRole and one string variable named as strUserRole. If you click on AdminRole button, you will get Administrator.siteMap because I am passing "Administrator" as hard coded or if you will click on UserRole you will get User.siteMap. Here I can't describe as per role because I am not logging or taking user privileges from anywhere.

In button event, we need to bind Menu1 again:

Define strUserRole globally like:

string strUserRole = string.Empty;

and inside button event:

protected void btnAdmin_Click(object sender, EventArgs e)
{
	strUserRole = "Administrator";
	Menu1.DataSource = GetDataSource(strUserRole, Server.MapPath("~"));
	Menu1.DataBind();
}
protected void btnUser_Click(object sender, EventArgs e)
{
	strUserRole = "User";
	Menu1.DataSource = GetDataSource(strUserRole, Server.MapPath("~"));
	Menu1.DataBind();
}

History

  • 22nd May, 2009: Initial post

License

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

About the Author

ahmad.msn

Software Developer
Acusis Software India Pvt Ltd.
India India

Member

I am Ahmad al mursalin working as a software engineer in Acusis software ind pvt ltd.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhy the menu don't display? Pinmemberjb1517:35 10 Jun '09  
AnswerRe: why the menu don't display? Pinmemberahmad.msn2:15 11 Jun '09  
GeneralASP.NET Site-Map Security Trimming is better PinmemberMinimon KANKARTI22:32 1 Jun '09  
QuestionWhy do all this? Pinmembercnoevil11:40 27 May '09  
GeneralNice one Pinmembersureshkumargundala21:04 24 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 May 2009
Article Copyright 2009 by ahmad.msn
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid