Home  • Programming • ASP.NET

How To create Dynamic Menus In Master Page ASP.NET

Step 1: First create an web application then using Microsoft Visual Studio. Step 2: Right click on Site.Master and then choose "View Markup" then clear all existing menu items inside the <asp:Menu> element. Step 3: Expend Site.Master from Solution Explorer. Step 4: Delete Site.Master.designer.cs Step 5: Right click on Site.Master then choose "Convert to Web Application" option and then Choose Yes Step 6: Right click on Site.Master.cs and then choose "View Code". Step 7: Then write the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
    public partial class SiteMaster : System.Web.UI.MasterPage
        protected void Page_Load(object sender, EventArgs e)
            MenuItem home = new MenuItem();
            home.Text = "Home";
            home.NavigateUrl = @"~/Default.aspx";

            MenuItem about = new MenuItem();
            about.Text = "About";
            about.NavigateUrl = "~/About.aspx";
            NavigationMenu.Items.Add(about); ;


Comments 1