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
="1.0"="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
="1.0"="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