asp.net c# tutorials and examples code for web developers

How to use Menu control in asp.net

Menu example: how to use Menu control in asp.net

Menu control is a Navigation Toolbox Control. Here i show a simple example of Menu control.

Web.sitemap
First you need to create a Web.sitemap file. Here is the source code of Web.sitemap file. This is the main file where we store all navigation url.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="Default.aspx" title="Home"  description="Home Page">
    <siteMapNode url="StandardToolBox.aspx" title="Standard ToolBox Control"  description="Standard ToolBox Control">
      <siteMapNode url="BulletedList.aspx" title="BulltedList Example"  description="BulltedList Control Simple Example" />
      <siteMapNode url="CheckBox.aspx" title="CheckBox Example"  description="CheckBox Control Simple Example" />
      <siteMapNode url="CheckBoxList.aspx" title="CheckBoxList Example"  description="CheckBoxList Control Simple Example" />
    </siteMapNode>
    <siteMapNode url="DataToolBox.aspx" title="Data ToolBox Control"  description="Data ToolBox Control">
      <siteMapNode url="SqlDataSource.aspx" title="SqlDataSource Example"  description="SqlDataSource Simple Example" />
      <siteMapNode url="XmlDataSource.aspx" title="XmlDataSource Example"  description="XmlDataSource Simple Example" />
    </siteMapNode>
    </siteMapNode>
</siteMap>
Place the Menu Control
Now create a WebForm name Default.aspx. Then place a SiteMapDataSource Control and a Menu Control. This is very simple file. Here is the source code of Default.aspx.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Menu Control Simple Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"></asp:Menu>
    </div>
    </form>
</body>
</html>


Run the Default.aspx file. When you click a link then it goes the specific page.





Related asp.net examples