Introduction
In this article, I am going to show how to easily generate menus using XML from the data source in Asp.net C#. By using this we can define the hierarchy separately in XML and use the same in the menus. Below is the real time code I have used in my project and I have attached the same in the attachment section.
Aspx page Code
- <asp:XmlDataSource ID="XmlDataSrc" runat="server" DataFile="App_Data/Tpms_Menus.xml" XPath="/menu/Admin/FirstlevelMenus"></asp:XmlDataSource>
- <asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSrc" Orientation="Horizontal" OnMenuItemClick="Menu1_MenuItemClick" Font-Names="Verdana" ForeColor="White" Width="800px" CssClass="MYMenuClass">
- <DataBindings>
- <asp:MenuItemBinding DataMember="FirstlevelMenus" TextField="textdis" NavigateUrlField="urllink" />
- <asp:MenuItemBinding DataMember="secondlevelMenus" TextField="txtdis" NavigateUrlField="secondlevelurl" />
- <asp:MenuItemBinding DataMember="thirdlevelMenus" TextField="txtdis" NavigateUrlField="thirdlevelurl" />
- </DataBindings>
- </asp:Menu>
You can place the same in the .master page, if you have multiple pages in the project and want the menu to appear in all the pages.
XML Code
- <?xml version="1.0" encoding="utf-8" ?>
- <menu>
- <Admin>
- <FirstlevelMenus textdis="Users" urllink="~/TP_Admin.aspx">
- <secondlevelMenus txtdis="ViewUsers" secondlevelurl="ViewUsers.aspx">
- </secondlevelMenus>
- <secondlevelMenus txtdis="UpdateUser" secondlevelurl="~/TP_Admin.aspx">
- <thirdlevelMenus txtdis="checkthirdlevel" thirdlevelurl="">
- </thirdlevelMenus>
- </secondlevelMenus>
- <secondlevelMenus txtdis="UnlockUser" secondlevelurl=""></secondlevelMenus>
- <secondlevelMenus txtdis="SetUserPrivelage" secondlevelurl=""></secondlevelMenus>
- <secondlevelMenus txtdis="ViewUsers" secondlevelurl=""></secondlevelMenus>
- </FirstlevelMenus>
- <FirstlevelMenus textdis="Values" urllink="">
- <secondlevelMenus txtdis="AddBOQ" secondlevelurl="">
- <thirdlevelMenus txtdis="ImportBOQ" thirdlevelurl=""></thirdlevelMenus>
- </secondlevelMenus>
- <secondlevelMenus txtdis="EditBOQ" secondlevelurl=""></secondlevelMenus>
- </FirstlevelMenus>
- <FirstlevelMenus textdis="Activity" urllink="">
- <secondlevelMenus txtdis="Activity_Groups" secondlevelurl="~/Estimation/CEO.aspx"> </secondlevelMenus>
- <secondlevelMenus txtdis="Activity_Items" secondlevelurl="/Estimation/Activity_Items.aspx"></secondlevelMenus>
- </FirstlevelMenus>
- <FirstlevelMenus textdis="Resources" urllink="">
- <secondlevelMenus txtdis="Materials" secondlevelurl="~/Estimation/Materials.aspx"></secondlevelMenus>
- <secondlevelMenus txtdis="Edittender" secondlevelurl="~/TPMS_ASPX/TSCM/Tender_Registry.aspx"></secondlevelMenus>
- </FirstlevelMenus>
- </Admin>
- </menu>

Conclusion
I hope the above information was useful. Kindly let me know your valuable feedback or thoughts.

Prasanna MuraliPosted Jun 26, 2016, 9:01 AM
Nice share...