Menu Control With XMLDataSource

The Menu control is a Databound control that is used to display hierarchical data in the form of a Menu system. The Menu Control can be populated using static data or by the databinding of the control.

In case of DataBinding, we can choose any DataSource which implements the IHierarchicalDataSource interface, . It simply set the DataSourceId property of the Menu Control to the ID value of the data source control and the Menu Control automatically binds to the specified data source Control.

The Menu Control Contains a DataBindings property that is a collection of MenuItemBinding objects that define the binding between a DataItems and the items of Menu.

Here I write a simple program to show the binding of XMLDataSource and Menu Control.

Step1: First we create a XML File.(in my case XMLFile.xml)

File :-> New :-> File :->XMLFile.xml

Step2: Write the following code in it.

<?xml version="1.0" encoding="utf-8" ?>
<MenuItems>
  <Home display="Home" url="~/" />
  <Menu display="Menu" url="~/Menu/">
    <Menu1 display="Menu1" url="~/Menu/Menu1.aspx" />
    <Menu2 display="Menu2" url="~/Menu/Menu2.aspx" />    
  </Menu>
    <Support display="Support" url="~/Support/" >
      <AboutUs display="AboutUs" url="~/Support/AboutUs.aspx" />
      <Downloads display="Downloads" url="~/Support/Downloads.aspx" />
    </Support>
    <AboutUs display="About Us" url="~/About Us/" >
      <Company display="Company" url="~/About Us/Company.aspx" />
      <Locations display="Locations" url="~/About Us/Locations.aspx" />
    </AboutUs>
</MenuItems>

Step3: An XMLDataSource, a Menu are added to the web page (Default.aspx in my case). The XMLDataSource is configured to use the XML File. The Menu Control is used to Configures to use the XMLDataSource.

<form id="form1" runat="server">
    <div>
    <asp:Menu ID="Menu1" runat="server" DataSourceID="XmldataSource1" ></asp:Menu>
    <asp:XmlDataSource ID="XmldataSource1" runat="server" DataFile="~/XMLFile.xml"
></asp:XmlDataSource>
    </div>
    </form>

Ebook Download
View all
Learn
View all