Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Data Base Driven Menu Item In ASP.NET

Article:
Viewed:  965 
Posted On:  29/01/2015 04:03:46 
How we can populate Menu Item from Data Base in asp.net c# ? 

In this article I am going to explain how we can populate Menu from Data Base in asp.net c#.

Below is my Menu Item Data Table in design Mode:

1.png

Image 1.

Below is the script of my table:

CREATE TABLE [dbo].[tbl_Menu](
          [Menu_ID] [int] IDENTITY(1,1) NOT NULL,
          [Menu_ParentID] [int] NULL,
          [Menu_Name] [varchar](50) NULL,
          [Menu_Page_URL] [varchar](500) NULL,
 CONSTRAINT [PK_tbl_Menu] PRIMARY KEY CLUSTERED
(
          [Menu_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON
PRIMARY]
) ON [PRIMARY]
GO

Now Data in my tbl_Menu Table:

2.png

Image 2.

Here in this application I am using a Master Pager to give a consistent look in my entire application. So I wrote code to generate Menu item from Data base in MasterPage.master.cs.

Below is my MasterPage.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MenuFromDB.MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheetMenu.css" rel="stylesheet" />
    <link href="StyleSheet.css" rel="stylesheet" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table cellpadding="1" cellspacing="1" width="880px" align="center" class="BlueBorder"
                style="background: White;">
                <tr>
                    <td style="height: 100px; background-color: skyblue; padding-left: 10px;">
                        <span style="font-size: 20pt; font-weight: bold; color: blue;">Data Base Driven Menu Item In ASP.NET C#</span>
                    </td>
                </tr>
                <tr>

                    <td style="background-color: orange; padding-left: 1px;">
                        <asp:Menu ID="MenuFromDB" runat="server" Orientation="Horizontal">
                            <LevelMenuItemStyles>
                                <asp:MenuItemStyle CssClass="Parent_Menu" />
                                <asp:MenuItemStyle CssClass="level_menu" />
                            </LevelMenuItemStyles>
                            <StaticSelectedStyle CssClass="selected" />
                        </asp:Menu>
                    </td>
               </tr>
                <tr>
                    <td style="padding-top: 40px; padding-bottom: 80px; padding-left: 10px">
                        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                        </asp:ContentPlaceHolder>
                    </td>
                </tr>
            </table>
 
        </div>
    </form>
</body>
</html>
 

Now MasterPage.Master.cs is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
namespace MenuFromDB
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dt = GetMenuDataFromDB(0);
                PopulateMenu(dt, 0, null);
            }
        }
 
        public DataTable GetMenuDataFromDB(int MenuParentID)
        {
            SqlConnection con = new SqlConnection();
            ds = new DataSet();
            con.ConnectionString = @"Data Source=MYPC\SqlServer2k8; Initial Catalog=Test; Integrated Security=true;";
            SqlCommand cmd = new SqlCommand("SELECT *  FROM tbl_Menu WHERE Menu_ParentID='" + MenuParentID + "'", con);
 
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            
return ds.Tables[0];
        } 

        private void PopulateMenu(DataTable dt, int Menu_Parent_ID, MenuItem Parent_MenuItem)
        {
            string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
            foreach (DataRow row in dt.Rows)
            {
                MenuItem menuItem = new MenuItem
                {
                    Value = row["Menu_Id"].ToString(),
                    Text = row[
"Menu_Name"].ToString(),
                    NavigateUrl = row["Menu_Page_URL"].ToString(),
                    Selected = row["Menu_Page_URL"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
                };

                if (Menu_Parent_ID == 0)
                {
                    MenuFromDB.Items.Add(menuItem);
                    DataTable dtChildMenu = new DataTable();
                    dtChildMenu = this.GetMenuDataFromDB(int.Parse(menuItem.Value));
                    PopulateMenu(dtChildMenu, int.Parse(menuItem.Value), menuItem);
                }
                else
                {
                    Parent_MenuItem.ChildItems.Add(menuItem);
                }
            }
        }
    }
}

Now Run the application:

3.png

Image 3.

4.png

Image 4.

Now hover on any menu and see child menu.

5.png

Image 5.

6.png

Image 6.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us