Dynamic Menu in Asp.Net Using Database

Dynamic Menu in Asp.Net Using Database

Hi friends, in this post we are discussing how to create Dynamic Menu in Asp.Net Using Database. Dynamic menu in asp.net is like adding links from backend or admin panel. In our earlier post we had already discussed about how to create dynamic menu in asp.net. On that we have used asp menu control to do this. here we are using PLACEHOLDER, also here we are using Bootstrap framework to make it responsive one too.

In our earlier posts we had discussed about Dynamic Title Keywords and Meta Description In Asp.netChange GridView Row Color Based on Value of Row, and Format of the initialization string does not conform to specification starting at index 0.

Dynamic Menu in Asp.Net

 

Dynamic Menu in Asp.Net Create table Query:

-----------Create Table------------
create table menu_master
(
menu_id int identity(1,1),
menu_name varchar(50),
menu_url nvarchar(100),
[enable] char(1),
order_id int,
)

-----Insert Dummy Data--------------

insert into menu_master values('Hightechnology','http://www.hightechnology.in','Y',1)
insert into menu_master values('Grootstech','http://www.grootstech.com','Y',2)
insert into menu_master values('google','http://www.google.co.in','Y',3)

Dynamic Menu in Asp.Net Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="css/bootstrap.min.css" rel="stylesheet" />
<title>Dynamic Menu in Asp.Net Using Database</title>
<meta name="description" content="Dynamic Menu in Asp.Net, how to create dynamic menu in asp.net, how to create dynamic menu in asp.net With Database, create dynamic menu in asp.net c#" />
</head>
<body>
<form id="form1" runat="server">
<div class="container" style="margin-top:50px">
<div class="row">
<div class="col-sm-12">
<h1 style="text-align:center">Dynamic Menu in Asp.Net Using Database</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12" style="margin-top:30px">
<div class="col-sm-4">

</div>
<div class="col-sm-4">
<asp:PlaceHolder ID="side_menu" runat="server">
<a href="#" class="list-group-item active">Categories</a>
</asp:PlaceHolder>
</div>
<div class="col-sm-4">

</div>
</div>
</div>  
<div class="row">
<div class="col-sm-12" style="margin-top:30px;text-align:center">
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>| Back to article:
<a href="http://hightechnology.in/dynamic-menu-in-asp-net-using-database/">
Dynamic Menu in Asp.Net Using Database</a> | Hosting partner 
<a href="http://www.grootstech.com" target="_blank">Grootstech</a> 
</div>
</div>
</div>
</form>
</body>
</html>

Dynamic Menu in Asp.Net Code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Index : System.Web.UI.Page
{
    String strcon = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        Binddata();
    }


    public void Binddata()
    {
        SqlConnection con = new SqlConnection(strcon);
        con.Open();
        String str = "select menu_name,menu_id,menu_url from menu_master where enable='y' order by order_id";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader drcategory = cmd.ExecuteReader();
        
        int n = 1;
        String categorydata = "<div class='list-group'>";
        if (drcategory.HasRows)
        {
            while (drcategory.Read())
            {
                categorydata += "<a href=" + drcategory[2].ToString() + " class='list-group-item'>" + drcategory[0].ToString() + "</a>";
            }
        }
        categorydata += "</div>";
        Label lbl_categorydata = new Label();
        lbl_categorydata.Text = categorydata.ToString();
        side_menu.Controls.Add(lbl_categorydata);
        con.Close();
    }
}