Tuesday, August 24, 2010

How to bind XML file in to asp.net Menu control

Example page:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string xmlPath = Server.MapPath("~/Navigation.xml");
            XmlDataSource xmlDS = new XmlDataSource();
            xmlDS.DataFile = xmlPath;
            string iRole = "1";
            xmlDS.XPath = "Home/Role[@id='" + iRole + "']";
            this.menuNavigation.DataSource = xmlDS;
            this.menuNavigation.DataBind();
            this.menuNavigation.Items[0].Text = "";
            this.menuNavigation.Items[0].Value = "";
            this.menuNavigation.Items[0].Selectable = false;            
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Menu 
        ID="menuNavigation" 
        runat="server" 
        StaticDisplayLevels="2"
        DynamicHorizontalOffset="1" 
        DynamicVerticalOffset="1"
        Orientation="Horizontal">
        <DataBindings>
            <asp:MenuItemBinding 
                DataMember="Menu" 
                TextField="text" 
                ValueField="text" 
                NavigateUrlField="url" />
            <asp:MenuItemBinding 
                DataMember="SubMenu" 
                NavigateUrlField="url" 
                TextField="text"
                ValueField="text" />
        </DataBindings>
    </asp:Menu>
    </form>
</body>
</html>

XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Home>
  <Role id="1">
    <Menu text="Item1" url="Item1.aspx">
      <SubMenu text="Page-1-1" url="Page-1-1.aspx" />
      <SubMenu text="Page-1-2" url="Page-1-2.aspx" />
      <SubMenu text="page-1-3" url="Page-1-3.aspx" />
    </Menu>
    <Menu text="Item2"  url="Item2.aspx">
      <SubMenu text="Page-2-1" url="Page-2-1.aspx">
        <SubMenu text="Page-2-1-1" url="Page-2-1-1.aspx">
          <SubMenu text="Page-2-1-1-1" url="Page-2-1-1-1.aspx" />
          <SubMenu text="Page-2-1-1-2" url="Page-2-1-1-2.aspx" />
          <SubMenu text="Page-2-1-1-3" url="Page-2-1-1-3.aspx" />
        </SubMenu>
        <SubMenu text="Page-2-1-2" url="Page-2-1-2.aspx"></SubMenu>
      </SubMenu>
      <SubMenu text="Page-2-2" url="Page-2-2.aspx">
        <SubMenu text="Page-2-2-1" url="ComedyPage-2-2-1.aspx">
          <SubMenu text="Page-2-2-1-1" url="Page-2-2-1-1.aspx" />
          <SubMenu text="Page-2-2-1-2" url="Page-2-2-1-2.aspx" />
          <SubMenu text="Page-2-2-1-3" url="Page-2-2-1-3.aspx" />
          <SubMenu text="Page-2-2-1-4" url="Page-2-2-1-4.aspx" />
        </SubMenu>
        <SubMenu text="Page-2-2-2" url="Page-2-2-2.aspx" />
        <SubMenu text="Page-2-2-3" url="Page-2-2-3.aspx" />
      </SubMenu>
    </Menu>
    <Menu text="Item3" url="Item3.aspx" />
  </Role>
  <Role id="2">
    <Menu text="Item4" url="Item4.aspx">
      <SubMenu text="Page-4-1" url="Page-4-1.aspx" />
      <SubMenu text="Page-4-2" url="Page-4-2.aspx" />
    </Menu>
    <Menu text="Item5" url="Item5.aspx" />
  </Role>
</Home>

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...