School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,096 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,005 people online right now. Registration is fast and FREE... Join Now!




ASP.Net Menu with XML DataSource

 
Reply to this topicStart new topic

> ASP.Net Menu with XML DataSource, Using XML for menu

eclipsed4utoo
Group Icon



post 10 Jul, 2009 - 11:37 AM
Post #1


I was recently tasked with creating a menu system that was dynamic depending on the user's security level. For example, a user that was part of an Administrator group would be able to see a menu item for the Administrator pages. User's who were not part of the Administrator group would not be able to see this menu.

So I decided to use the ASP.Net Menu and use an XML Datasource as it's datasource.

So first, you have your ASP.Net Menu....

html

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" DisappearAfter="10"
Width="300px" DataSourceID="XmlDataSource1" StaticEnableDefaultPopOutImage="False">
<StaticMenuItemStyle CssClass="MenuItem" />
<DynamicHoverStyle CssClass="SubMenuItemHover" />
<DynamicMenuItemStyle CssClass="SubMenuItem" />
<StaticHoverStyle CssClass="MenuItemHover" />
<DataBindings>
<asp:MenuItemBinding DataMember="Item" NavigateUrlField="Url" TextField="Text" />
</DataBindings>
</asp:Menu>


then we have our XML datasource object...

html

<asp:XmlDataSource ID="XmlDataSource1" runat="server" />


Notice that the "DataSourceID" of the Menu is set to the XMLDataSource.

Also notice the "DataBindings" attributes. This will determine the Text and URL of the Menu items.

So now we have our XML files....

Administrator file (admin_menu.xml)
xml

<?xml version="1.0" encoding="utf-8" ?>
<Items Text="">
<Item Text="Home" Url="~/default.aspx" />
<Item Text="Time Entry" Url="">
<Item Text="Time Entry Page" Url="~/TimeEntryPage.aspx" />
</Item>
<Item Text="Admin" Url="">
<Item Text="Move Time Entry" Url ="~/MoveTimeEntry.aspx" />
</Item>
</Items>


Normal User file (user_menu.xml)
xml

<?xml version="1.0" encoding="utf-8" ?>
<Items Text="">
<Item Text="Home" Url="~/default.aspx" />
<Item Text="Time Entry" Url="">
<Item Text="Time Entry Page" Url="~/TimeEntryPage.aspx" />
</Item>
</Items>


As you can see, the Administrator file has an entry for the "Admin" menu and the Normal User file does not.

Now in the code behind, we will determine if the user is an Administrator or not.

c#

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string appDataPath = HttpContext.Current.Server.MapPath("~/App_Data");
this.XmlDataSource1.DataFile = appDataPath + "\\" + GetRole() + "_menu.xml";
this.XmlDataSource1.XPath = @"/Items/Item";
}
}


and the GetRole() method
c#

// This method checks against a database field to determine if user
// is an Admin
// You could also integrate with Active Directory to see if user
// is part of a specific AD user group
// The Session "OperatorID" is stored from a login page
protected string GetRole()
{
string role = string.Empty;
int operatorID = 0;

operatorID = int.Parse(Session["OperatorID"].ToString());

if (OperatorManager.IsOperatorAdmin(operatorID))
role = "Admin";
else
role = "User";

return role;
}


And that's it.

Now the benefit of this is that you can change the menus that users will see by simply editing the xml files(assuming the .aspx pages are already created). For example, if I wanted all users to see the Admin menu, I would simply change the xml file on the web server. No need to build and publish the site again.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 11:49AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month