Click here to Skip to main content
15,868,049 members
Articles / Web Development / ASP.NET

Binding Sitemaps with Menu Control Based on User Privilege at Runtime

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
22 May 2009CPOL2 min read 47.4K   1K   22   7
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
<?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
<?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.NET
<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():

C#
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.

C#
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:

C#
string strUserRole = string.Empty;

and inside button event:

C#
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)


Written By
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.

Comments and Discussions

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

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.