Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 155 - Binding menu control to an xml file using xmldatasource control

Suggested Videos
Part 152 - Master page content page user control life cycle
Part 153 - Menu control in
Part 154 - Using styles with menu control

In this video, we will discuss binding menu control to an xml file using xmldatasource control. This is continuation to Part 154, please watch Part 154 before proceeding.

1. Add an XML file and name it MenuData.xml. Copy and paste the following xml.
<?xml version="1.0" encoding="utf-8" ?>
  <MenuItem NavigateUrl="~/Home.aspx" Text="Home"/>
  <MenuItem NavigateUrl="~/Employee.aspx" Text="Employee">
    <MenuItem NavigateUrl="~/UploadResume.aspx" Text="Upload Resume"/>
    <MenuItem NavigateUrl="~/EditResume.aspx" Text="Edit Resume"/>
    <MenuItem NavigateUrl="~/ViewResume.aspx" Text="View Resume"/>
  <MenuItem NavigateUrl="~/Employer.aspx" Text="Employer">
    <MenuItem NavigateUrl="~/UploadJob.aspx" Text="Upload Job"/>
    <MenuItem NavigateUrl="~/EditJob.aspx" Text="Edit Job"/>
    <MenuItem NavigateUrl="~/ViewJob.aspx" Text="View Job"/>
  <MenuItem NavigateUrl="~/Admin.aspx" Text="Admin">
    <MenuItem NavigateUrl="~/AddUser.aspx" Text="Add User"/>
    <MenuItem NavigateUrl="~/EditUser.aspx" Text="Edit User"/>
    <MenuItem NavigateUrl="~/ViewUser.aspx" Text="View User"/>

2. Drag and drop an XmlDataSource control on the webform. Set XPath and DataFile attributes as shown below. Notice that DataFile attribute points to the XML file that we added in Step 1.
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
    XPath="/Items/MenuItem" DataFile="~/MenuData.xml">

3. Drag and drop a menu control and set DataSourceID attribute to the xmldatasource control we created in Step 2. Also, set DataBindings as shown below.
<asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1"
        <asp:MenuItemBinding DataMember="MenuItem" 
            NavigateUrlField="NavigateUrl" TextField="Text" />

4. To set the styles for the selected menu item, copy and paste the following code in the code-behind file.
private void Check(MenuItem item)
    if (item.NavigateUrl.Equals(Request.AppRelativeCurrentExecutionFilePath, 
        item.Selected = true;
    else if (item.ChildItems.Count > 0)
        foreach (MenuItem menuItem in item.ChildItems)

protected void Menu1_PreRender(object sender, EventArgs e)
    foreach (MenuItem item in Menu1.Items)

1 comment:

  1. Hi Venkat,
    If u dont mind can u upload the video of Agile methodlogy in SDLC.It is very important video for beginners and every one who are in the field.I hope u will upload verysoon.
    Thankyou for your videos


It would be great if you can help share these free resources