Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://getcode.in/2012/04/create-dynamic-asp-net-menus-from-database/" rel="nofollow">Download Source Code Here</a> </p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;DataBase Driven Menu&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div id="myslidemenu" class="jqueryslidemenu"&gt; &lt;asp:Menu ID="Menu1" runat="server" StaticEnableDefaultPopOutImage="False" Orientation="Horizontal" StaticSubMenuIndent="10px" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000"&gt; &lt;DataBindings&gt; &lt;asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text" ToolTipField="ToolTip" /&gt; &lt;/DataBindings&gt; &lt;DynamicHoverStyle BackColor="#990000" ForeColor="White" /&gt; &lt;DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /&gt; &lt;DynamicMenuStyle BackColor="#FFFBD6" /&gt; &lt;DynamicSelectedStyle BackColor="#FFCC66" /&gt; &lt;StaticHoverStyle BackColor="#990000" ForeColor="White" /&gt; &lt;StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /&gt; &lt;StaticSelectedStyle BackColor="#FFCC66" /&gt; &lt;/asp:Menu&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>In Code Behind File</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Xml; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet ds = new DataSet(); XmlDataSource xmlDataSource = new XmlDataSource(); xmlDataSource.ID = "xmlDataSource"; xmlDataSource.EnableCaching = false; string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Admin\WebSite28\App_Data\Database.mdf;Integrated Security=True;User Instance=True"; using (SqlConnection conn = new SqlConnection(connStr)) { string sql = "Select ID, Text,NavigateUrl,ParentID from Menu"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); da.Fill(ds); da.Dispose(); } &amp;nbsp; ds.DataSetName = "Menus"; ds.Tables[0].TableName = "Menu"; DataRelation relation = new DataRelation("ParentChild", ds.Tables["Menu"].Columns["ID"], ds.Tables["Menu"].Columns["ParentID"], true); relation.Nested = true; ds.Relations.Add(relation); xmlDataSource.Data = ds.GetXml(); //Reformat the xmldatasource from the dataset to fit menu into xml format xmlDataSource.TransformFile = Server.MapPath("~/TransformXSLT.xsl"); //assigning the path to start read all MenuItem under MenuItems xmlDataSource.XPath = "MenuItems/MenuItem"; //Finally, bind the source to the Menu1 control Menu1.DataSource = xmlDataSource; Menu1.DataBind(); } } } </code></pre> <p>Create an TransformXSLT.xsl</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" encoding="utf-8"/&gt; &lt;!-- Find the root node called Menus and call MenuListing for its children --&gt; &lt;xsl:template match="/Menus"&gt; &lt;MenuItems&gt; &lt;xsl:call-template name="MenuListing" /&gt; &lt;/MenuItems&gt; &lt;/xsl:template&gt; &lt;!-- Allow for recusive child node processing --&gt; &lt;xsl:template name="MenuListing"&gt; &lt;xsl:apply-templates select="Menu" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Menu"&gt; &lt;MenuItem&gt; &lt;!-- Convert Menu child elements to MenuItem attributes --&gt; &lt;xsl:attribute name="Text"&gt; &lt;xsl:value-of select="Text"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:attribute name="ToolTip"&gt; &lt;xsl:value-of select="ToolTip"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:attribute name="NavigateUrl"&gt; &lt;xsl:value-of select="NavigateUrl"/&gt; &lt;/xsl:attribute&gt; &lt;!-- Call MenuListing if there are child Menu nodes --&gt; &lt;xsl:if test="count(Menu) &gt; 0"&gt; &lt;xsl:call-template name="MenuListing" /&gt; &lt;/xsl:if&gt; &lt;/MenuItem&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Create Sql Table</p> <pre><code>CREATE TABLE [dbo].[Menu]( [ID] [int] IDENTITY(1,1) NOT NULL, [Text] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ToolTip] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [NavigateUrl] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ParentID] [int] NULL) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload