locked
How Dynamically Make and show ASP.NET Menu Items using XML Datasource? RRS feed

  • Question

  • User1145956344 posted

    i want to make asp.net menu dynamically using ms sql 2005 db   i could just find these codes for getting items in the hierarchical schema,any one knows how to bind it to asp.net menu using XML datasource?

          SqlDataAdapter DA = new SqlDataAdapter("exec [GetCats]",DatabaseConnection);
          DA.Fill(DS);
          DataRelation DR = new DataRelation("InnerJoin", DS.Tables[0].Columns["MenuID"], DS.Tables[0].Columns["ParentID"], true);
          DR.Nested = true;
          DS.Relations.Add(DR);

           DS.GetXML();

     

    thanx
     

    Monday, January 21, 2008 11:31 AM

Answers

  • User582801490 posted

     Code to be put in ASPX file....

    <asp:Menu ID="Menu1" runat="server" MaximumDynamicDisplayLevels="4" StaticDisplayLevels="2"
                DynamicHorizontalOffset="1" DynamicVerticalOffset="1" StaticEnableDefaultPopOutImage="false"
                Orientation="Horizontal">
                <StaticHoverStyle CssClass="menuBarHover" />
                <DynamicHoverStyle CssClass="menuBarHover" />
                <StaticMenuItemStyle CssClass="menuItem" />
                <DynamicMenuItemStyle CssClass="menuItemDynamic" />
                <DataBindings>
                    <asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="text" NavigateUrlField="url" />
                    <asp:MenuItemBinding DataMember="SubMenu" NavigateUrlField="url" TextField="text"
                        ValueField="text" />
                </DataBindings>
            </asp:Menu>

    Code to be put in ASPX.CS file (server side code)

    string xmlPath = Server.MapPath("~/App_Data/XMLMenu.xml");

            XmlDataSource xmlDS = new XmlDataSource();
            xmlDS.DataFile = xmlPath;
           
            xmlDS.XPath = "Home/Role[@id='" + iRole + "']";

            Menu1.DataSource = xmlDS;
            Menu1.DataBind();
            Menu1.Items[0].Text="";
            Menu1.Items[0].Value="";
            Menu1.Items[0].Selectable=false;

    Sample XML File Used in this Code 

    <?xml version="1.0" encoding="utf-8" ?>
    <Home>
        <Role id="1">
            <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>
        </Role>
        <Role id="2">
            <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="Contact Us" url="MenuFromXml.aspx"></Menu>
        </Role>
    </Home>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2008 12:07 PM
  • User1702256038 posted

    Hi,

    Do you want to generate a menu from the sqldatabase and get the items in the hierarchical schema, then bind it to asp.net menu using XML datasource?

    You can try to check this article: http://aspalliance.com/822

    If you want to Make and show ASP.NET Menu Items using XML Datasource dynamically, you should set the xmldatasource's property  EnableCaching is False, its default value is true.

     

    Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2008 1:05 AM

All replies

  • User582801490 posted

     Code to be put in ASPX file....

    <asp:Menu ID="Menu1" runat="server" MaximumDynamicDisplayLevels="4" StaticDisplayLevels="2"
                DynamicHorizontalOffset="1" DynamicVerticalOffset="1" StaticEnableDefaultPopOutImage="false"
                Orientation="Horizontal">
                <StaticHoverStyle CssClass="menuBarHover" />
                <DynamicHoverStyle CssClass="menuBarHover" />
                <StaticMenuItemStyle CssClass="menuItem" />
                <DynamicMenuItemStyle CssClass="menuItemDynamic" />
                <DataBindings>
                    <asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="text" NavigateUrlField="url" />
                    <asp:MenuItemBinding DataMember="SubMenu" NavigateUrlField="url" TextField="text"
                        ValueField="text" />
                </DataBindings>
            </asp:Menu>

    Code to be put in ASPX.CS file (server side code)

    string xmlPath = Server.MapPath("~/App_Data/XMLMenu.xml");

            XmlDataSource xmlDS = new XmlDataSource();
            xmlDS.DataFile = xmlPath;
           
            xmlDS.XPath = "Home/Role[@id='" + iRole + "']";

            Menu1.DataSource = xmlDS;
            Menu1.DataBind();
            Menu1.Items[0].Text="";
            Menu1.Items[0].Value="";
            Menu1.Items[0].Selectable=false;

    Sample XML File Used in this Code 

    <?xml version="1.0" encoding="utf-8" ?>
    <Home>
        <Role id="1">
            <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>
        </Role>
        <Role id="2">
            <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="Contact Us" url="MenuFromXml.aspx"></Menu>
        </Role>
    </Home>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2008 12:07 PM
  • User1702256038 posted

    Hi,

    Do you want to generate a menu from the sqldatabase and get the items in the hierarchical schema, then bind it to asp.net menu using XML datasource?

    You can try to check this article: http://aspalliance.com/822

    If you want to Make and show ASP.NET Menu Items using XML Datasource dynamically, you should set the xmldatasource's property  EnableCaching is False, its default value is true.

     

    Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2008 1:05 AM
  • User1560559919 posted

    How Dynamically Make and show ASP.NET Menu Items using SQL Datasource????

    plz help...

    Saturday, October 30, 2010 1:44 AM