Wednesday 25 December 2013

Adding Menuitem to master page using Xml in ASP.NET

In this article we are going to see how to add the menu item to the master page using xml instead of static menu data. For that we have to create a xml which have following structure but the names for the tag and the attribute may be any thing and the submenu are given as child tag inside the menu.

XML Format:

<?xml version="1.0" encoding="utf-8" ?>
<Menus>
  <Menu value="Home" url="~/Home.aspx" />
  <Menu value="Employee" url="~/complete.aspx">
    <Menu value="Add Employee" url="~/Blog.aspx" />
  </Menu>
  <Menu value="Admin" url="Gallery.aspx">
    <Menu value="Add User" url="~/adduser.aspx" />
  </Menu>
</Menus>



HTML Code:
From the below code xmldatasource is used to bind the xml , DataFile is to bind the path ,XPath is to mention from where the iteration of menu starts.

DataBindings is the element is to bind the xml element as MenuItem.

DataMember : Which is element to consider as Menuitem data bind
TextField : Which is used to specify the attribute from where the Text field value is to be taken
NavigateUrlField : Which is used to specify the attribute from where the NavigateUrlField value is to be taken.

<asp:Menu DataSourceID="XmlDataSource1"  StaticMenuItemStyle-ItemSpacing="30px" DisappearAfter="2000"   runat="server" Orientation="Horizontal">
        <DataBindings>
         <asp:MenuItemBinding DataMember="Menu" TextField="value" NavigateUrlField="url" />
        </DataBindings>
    </asp:Menu>
      
        <asp:XmlDataSource ID="XmlDataSource1" DataFile="~/Menus.xml" XPath="Menus/Menu" runat="server"></asp:XmlDataSource>




Output: MenuItem





From this article you will learn how to add the menu item through xml to the master page.

No comments:

Post a Comment