Click here to Skip to main content
11,637,415 members (66,079 online)
Click here to Skip to main content

Binding Sitemaps with Menu Control Based on User Privilege at Runtime

, 22 May 2009 CPOL 27.3K 887 22
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

ahmad.msn
Software Developer Acusis Software India Pvt Ltd.
India India
I am Ahmad al mursalin working as a software engineer in Acusis software ind pvt ltd.

You may also be interested in...

Comments and Discussions

 
QuestionMenu not seen Pin
Member 1128870018-Jan-15 19:35
memberMember 1128870018-Jan-15 19:35 
QuestionMenu Not Displaying Pin
Reagan Gallant29-May-13 4:22
groupReagan Gallant29-May-13 4:22 
Questionwhy the menu don't display? Pin
jb1510-Jun-09 17:35
memberjb1510-Jun-09 17:35 
AnswerRe: why the menu don't display? Pin
ahmad.msn11-Jun-09 2:15
memberahmad.msn11-Jun-09 2:15 
GeneralASP.NET Site-Map Security Trimming is better Pin
Minimon KANKARTI1-Jun-09 22:32
memberMinimon KANKARTI1-Jun-09 22:32 
QuestionWhy do all this? Pin
cnoevil27-May-09 11:40
membercnoevil27-May-09 11:40 
GeneralNice one Pin
sureshkumargundala24-May-09 21:04
membersureshkumargundala24-May-09 21:04 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 22 May 2009
Article Copyright 2009 by ahmad.msn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid