AspdotnetCodes.com
Voted 'Best ASP.NET Host' for 2008 – DiscountASP.NET
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 Asp.Net Menu Control Binding with XMLDataSource
Posted by Moderator1 on 6/21/2007 12:07:55 PM Category: Asp.Net 2.0
Total Views : 22678
Add to my favorites
Email to friend
  
Introduction
This article explains the concept of building dynamic navigation control in web applications using Asp.Net Menu Control binding with XMLDataSource.
Description
Asp.Net Menu Control is a new and coolest navigation control. By using this we can build the navigation system of a website very dynamically. Even complex navigation can be made simple by using this Menu Control. This Menu Control can be completely collapsed or completely expanded down to any level. This Menu Control is an ideal control when you have lots of options for the user to choose. You can customize the look and feel of the menu control as you like by defining the properties of the control.
The Menu Control can bind with the datasource controls such as SiteMapDataSource and XMLDataSource. When your web application contains many complex dynamic links, you can manage a XML file to organize the links and bind the XML file to the Menu control with the help of XMLDataSource. This article demonstrates the way to bind the XMLDatasource control with a Menu Control. For example, we are going to create a Menu Control for a Book Store with various levels of related items.
XML File

Open a web application in Visual Studio 2005, add a XML file, named it as MenuList.xml. By default the xml file will be placed as App_Data folder in your solution. You have to define the navigation structure of the xml clearly so that it will be easier for you or the user to navigate. For testing this article you can copy the xml structure given below.

<?xml version="1.0" encoding="utf-8" ?>
<Home>
  <Menu text="Books" url="MenuFromXml.aspx">
    <SubMenu text="Asp.Net" url="MenuFromXml.aspx"></SubMenu>
    <SubMenu text="Ajax" url="MenuFromXml.aspx"></SubMenu>
    <SubMenu text="MS SQL Server 2005" url="MenuFromXml.aspx"></SubMenu>
    <SubMenu text="JavaScript" url="MenuFromXml.aspx"></SubMenu>
  </Menu>
  <Menu text="Electronics"  url="MenuFromXml.aspx">
    <SubMenu text="Camera" url="MenuFromXml.aspx">
      <SubMenu text="Digital" url="MenuFromXml.aspx">
        <SubMenu text="Canon" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="Kodak" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="Sony" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="Casio" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="Fuji" url="MenuFromXml.aspx"></SubMenu>
      </SubMenu>
      <SubMenu text="Film Camera" url="MenuFromXml.aspx"></SubMenu>
    </SubMenu>
    <SubMenu text="DVDs" url="MenuFromXml.aspx">
      <SubMenu text="Comedy" url="MenuFromXml.aspx">
        <SubMenu text="English" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="French" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="German" url="MenuFromXml.aspx"></SubMenu>
        <SubMenu text="Spanish" url="MenuFromXml.aspx"></SubMenu>
      </SubMenu>
      <SubMenu text="Kids Movies" url="MenuFromXml.aspx"></SubMenu>
      <SubMenu text="Romance Movies" url="MenuFromXml.aspx"></SubMenu>
      <SubMenu text="Action Movies" url="MenuFromXml.aspx"></SubMenu>
    </SubMenu>
  </Menu>
  <Menu text="Contact Us" url="MenuFromXml.aspx"></Menu>
</Home>


When you closely look at the xml structure above, the root element is 'Home', its child node is 'Menu' holds the attributes for Main Category and its child node is SubMenu, which contains the attibutes details for sub-categories. Each node contains value for attributes such as text and url. When you are creating your own xml file, atleast these two attributes are necessary, make sure that you have these attributes to bind with the Menu Control.

XMLDataSource

Drag and drop XMLDataSource Control to the page from the Toolbox's Data tab. Specify the DataFile property to the Xml file.

Asp.Net Menu Control

Drag and drop a Menu Control to your .aspx page from the Toolbox's Navigation tab. Specify the DataSourceID as XmlDataSource1. In the property tab of Menu Control, under Data, click on DataBindings. Here you can see all the available data bindings in your datasource (i.e. from your xml file). Click on Menu and add it down. Now set the TextField property as "text", ValueField property as "text" and NavigateUrlField as "url". Same way click on the SubMenu in available data bindings, and add it to the selected databindings section. Specify the TextField property as "text", ValueField property as "text" and NavigateUrlField as "url".
<asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1" 
MaximumDynamicDisplayLevels="4" StaticDisplayLevels="1"
DynamicHorizontalOffset="1" DynamicVerticalOffset="1"> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DynamicHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White" /> <DynamicMenuStyle BackColor="#FFFBD6" /> <StaticSelectedStyle BackColor="#FFCC66" /> <DynamicSelectedStyle BackColor="#FFCC66" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DataBindings> <asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="text"
NavigateUrlField="url" /> <asp:MenuItemBinding DataMember="SubMenu" NavigateUrlField="url" TextField="text" ValueField="text" /> </DataBindings> <StaticHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White" /> </asp:Menu> 
That's it. Save the changes and run the application. You can see the excellence of the Asp.Net Menu Control. Now you can make any attribute value changes in the xml file, which will immediately affect the Menu Control. In this way you can manage the complex navigation of your website in an easy way.

You can apply different styles to the Menu Control, by defining the properties such as StaticMenuItemStyle, DynamicMenuItemStyle, DynamicHoverStyle, StaticHoverStyle, etc., to your own desired layout. Come on, start to design your own menu now.


View our sample Menu Control.
 
Viewer's Comments
Posted by latha on 6/26/2007 5:31:29 AM
how to use xml data in asp.net with c#
 
Posted by Girish on 8/14/2007 12:24:59 AM
great help
 
Posted by Dinesh on 8/25/2007 8:44:26 AM
Nice , but no Idea about xml manipulation .
 
Posted by marcelo on 8/25/2007 12:47:16 PM
When I execute this teste, it is appearing only Home option in menu, and the others options are inside... What's happen?
 
Posted by Dinesh on 8/29/2007 4:11:28 AM
Root Tag is not suppose to display.It is usefull different file navigation but not enough for single file data retrieving....
 
Posted by Millstream on 9/17/2007 8:42:58 AM
How could I modify this code so that the root node "home" doesn't show?
 
Posted by Jose on 9/21/2007 3:41:49 PM
You can use XPath="/Home/Menu" to not sow the Home Item
 
Posted by Atul Chaudhari on 10/1/2007 2:11:21 AM
It is really a good article.
 
Posted by Lach on 12/18/2007 11:15:24 PM
This is how i implemented my menu a while back, now i want to add authorization for different users, i.e restrict some menu items to certain users, can i add another field to each page with an auth level and get the menu to restrict menu items depending on users level of auth ?
 
Posted by sds on 12/24/2007 6:29:28 AM
This example works fine. But it shows only Home as root menu and if we hover it shows others as submenus. I want to show the submenus mentioned here like Books, Electronics, DVDs etc., as main menu. If I do so it gives runtime error as cannot contain multiple root element. Please advise.
 
Posted by Shahid on 1/4/2008 1:20:31 AM
I'm also having the same problem i.e. The entire menu is represented by the Tag name instead of the "Text" attribute of Menu and Submenus. Anybody/Moderator can help in this regard??? Thanks in advance...
 
Posted by Shalini on 1/14/2008 12:01:55 AM
This example works fine & help me lot to create menu item as i m new in asp.net
 
Posted by Avinash Joshi on 1/22/2008 6:40:48 AM
Good Help Also Give it is with Coding like C#
 
Posted by Richard Hunt on 1/22/2008 9:42:05 AM
To hide the "Home" menu and display the submenus at the top, the only way I have found is to set the Text property of the Home item in the data bindings to a single space and the Selectable property to False. Then set the StaticDisplayLevels property of the menu control to 2. Does anyone know a better way to achieve this?
 
Posted by Richard Hunt on 1/22/2008 9:45:58 AM
Sorry, regarding hiding the "Home" menu, I've just noticed Jose's comment about setting the XPath property of the data source to /Home/Menu. This works perfectly.
 
Posted by abinash on 2/21/2008 9:02:00 AM
I want to have a Menu which will have nothing in UI when I open the Page it will dynamically creates the Menu and show in the Menubar in the application .can u provide me any source code . I will be thankful
 
Posted by Howzilla on 2/29/2008 1:55:21 PM
This does NOT work. 1) the menu items are stacked vertically. Every example I have found stacks vertically. NO ONE WANTS THAT. 2) The Sub-SubMenus DO NOT show. This is a flawed control from what I have seen.
 
Posted by ss on 3/7/2008 5:09:18 AM
poor
 
Posted by ss on 3/7/2008 5:10:54 AM
poor
 
Posted by tconn on 3/12/2008 11:59:04 AM
Why is it that when I try this all I see in my menu is the name of the XML node and not the attributes of the node which I've assigned to the TextField, ValueField, and NavigateUrlField? I also agree with the comment about the vertically stacking menu. That's not what I'm trying to get out of this. If there's some way to make the menu render horizontally, I'd appreciate the pointers.
 
Posted by scott on 3/20/2008 6:54:25 PM
Set Orientation on the menu control to Horizontal to make a Horizontal Menu
 
Posted by john on 3/24/2008 12:58:40 PM
RE: Rowzilla If you new how to use the control you wouldn't have said those things changing the orientation is simple, sub menus show just fine. Do it correctly and it works!
 
Posted by ZooZ on 3/29/2008 9:19:09 AM
Dont touch
 
Posted by RascaL on 4/2/2008 3:48:24 AM
Hey guys Firstly, try this as mentioned throughly. It works fine and perfectly.
 
Posted by Mutt on 4/2/2008 2:39:30 PM
Like this. I'm using it to create a menu for my Admin pages (located in a sub-folder. What I can't work out is how to get the css class "Asp-NetMenu-Selected" which you get on the "current" page if you use a sitemap datasource.
 
Posted by sumesh on 5/5/2008 5:19:05 AM
how create xmldatasourse and how bind to menu
 
Posted by Lalitendu Dash on 5/9/2008 5:56:04 AM
It is supports custom label design. but to change the direction not support so. can you help us? no doubt it is a very valuable article which i include in my application to generate dynamic menu using dynamic XML data from the databse.
 
Posted by 56 on 5/16/2008 4:05:45 AM
6
 
Posted by cxcxzc on 6/26/2008 1:08:21 AM
xzcxzc
 
Posted by Sathish on 6/26/2008 6:19:00 AM
how can i display in Horizontal?
 
Posted by Andorra on 6/27/2008 9:46:09 PM
Nice Site! http://google.com
 
Posted by abdulquddusa on 7/1/2008 7:55:50 AM
This is working but before a submenu item i want to display an image also how this can be achieved
 
Posted by abdulquddusa on 7/1/2008 7:59:25 AM
To make display all other items select the properties and in it set the StaticDisplayLevels to 2
 
Posted by abdulquddusa on 7/1/2008 8:02:00 AM
To Display in horizontal set the menu Orientation Property to Horizontal
 
Posted by France on 7/1/2008 6:40:09 PM
Nice Site! http://excellent-credit-card.blogspot.com