----------------------------------------Page Design-------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicMenu.aspx.cs" Inherits="MyTestLearnProject.DynamicMenu" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.menu{
width: auto;
margin: 0px auto;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 14px;
}
.menu ul li a:link, div ul li a:visited {
display: block;
background-color: #f1f1f1;color:#000;
text-align: center;
text-decoration: none;
padding: 4px;
border-bottom: 1px solid #fff;
width: 150px;
}
.menu ul li a:hover{
background-color: #ccc;
}
.menu ul li ul li a:link, li ul li a:visited {
display: block;
background-color: #f1f1f1;
color: #000;
text-align: center;
text-decoration: none;
padding: 4px;
border-bottom: 1px solid #fff;
width: 150px;
}
.menu ul li ul li a:hover {
background-color: #ccc;
}
.menu ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
.menu ul li {
float: left;
margin-left: 5px;
}
.menu ul li ul li {
float: none;
margin-left: 0px;
}
.menu ul li ul {
display: none;
}
.menu li:hover ul{
display: block;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:repeater ID="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound">
<headertemplate>
<div class="menu" id="gn" runat="server">
<ul>
</ul>
</div>
</headertemplate>
<itemtemplate>
<div class="menu" id="gn" runat="server">
<ul>
<li>
<a href='<%#Eval("MenuUrl") %>'><%#Eval("CategoryName") %></a>
<asp:literal ID="ltrlSubMenu" runat="server"></asp:literal>
</li>
</ul>
</div>
</itemtemplate>
<footertemplate>
</footertemplate>
</asp:repeater>
</form>
</body>
</html>
-----------------------------------------------------Back End 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;
using BE;
using BLL;
using System.IO;
using System.Text;
namespace MyTestLearnProject
{
public partial class DynamicMenu : System.Web.UI.Page
{
DataTable allCategories = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadCategories();
}
}
public void LoadCategories()
{
allCategories = new menuBLL().GetAllCategories();
rptCategories.DataSource = new menuBLL().GetCategories();
rptCategories.DataBind();
}
protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (allCategories != null)
{
DataRowView drv = e.Item.DataItem as DataRowView;
string ID = drv["ID"].ToString();
DataRow[] rows = allCategories.Select("ParentID=" + ID, "CategoryName");
if (rows.Length > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach (var item in rows)
{
sb.Append("<li><a href='"+item["MenuUrl"]+"'>" + item["CategoryName"] + "</a></li>");
}
sb.Append("</ul>");
(e.Item.FindControl("ltrlSubMenu") as Literal).Text = sb.ToString();
}
}
}
}
}
}
----------------------------------------Menu DAL--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class menuDAL
{
private string path = MyConnection.path;
public DataTable GetCategories()
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("select ID,CategoryName,MenuUrl from tblMenu where ParentID=0", con);
DataTable dt = new DataTable();
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
dt.Load(sdr);
}
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return dt;
}
public DataTable GetAllCategories()
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("select * from tblMenu", con);
DataTable dt = new DataTable();
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
dt.Load(sdr);
}
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return dt;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicMenu.aspx.cs" Inherits="MyTestLearnProject.DynamicMenu" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.menu{
width: auto;
margin: 0px auto;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 14px;
}
.menu ul li a:link, div ul li a:visited {
display: block;
background-color: #f1f1f1;color:#000;
text-align: center;
text-decoration: none;
padding: 4px;
border-bottom: 1px solid #fff;
width: 150px;
}
.menu ul li a:hover{
background-color: #ccc;
}
.menu ul li ul li a:link, li ul li a:visited {
display: block;
background-color: #f1f1f1;
color: #000;
text-align: center;
text-decoration: none;
padding: 4px;
border-bottom: 1px solid #fff;
width: 150px;
}
.menu ul li ul li a:hover {
background-color: #ccc;
}
.menu ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
.menu ul li {
float: left;
margin-left: 5px;
}
.menu ul li ul li {
float: none;
margin-left: 0px;
}
.menu ul li ul {
display: none;
}
.menu li:hover ul{
display: block;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:repeater ID="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound">
<headertemplate>
<div class="menu" id="gn" runat="server">
<ul>
</ul>
</div>
</headertemplate>
<itemtemplate>
<div class="menu" id="gn" runat="server">
<ul>
<li>
<a href='<%#Eval("MenuUrl") %>'><%#Eval("CategoryName") %></a>
<asp:literal ID="ltrlSubMenu" runat="server"></asp:literal>
</li>
</ul>
</div>
</itemtemplate>
<footertemplate>
</footertemplate>
</asp:repeater>
</form>
</body>
</html>
-----------------------------------------------------Back End 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;
using BE;
using BLL;
using System.IO;
using System.Text;
namespace MyTestLearnProject
{
public partial class DynamicMenu : System.Web.UI.Page
{
DataTable allCategories = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadCategories();
}
}
public void LoadCategories()
{
allCategories = new menuBLL().GetAllCategories();
rptCategories.DataSource = new menuBLL().GetCategories();
rptCategories.DataBind();
}
protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (allCategories != null)
{
DataRowView drv = e.Item.DataItem as DataRowView;
string ID = drv["ID"].ToString();
DataRow[] rows = allCategories.Select("ParentID=" + ID, "CategoryName");
if (rows.Length > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach (var item in rows)
{
sb.Append("<li><a href='"+item["MenuUrl"]+"'>" + item["CategoryName"] + "</a></li>");
}
sb.Append("</ul>");
(e.Item.FindControl("ltrlSubMenu") as Literal).Text = sb.ToString();
}
}
}
}
}
}
----------------------------------------Menu DAL--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class menuDAL
{
private string path = MyConnection.path;
public DataTable GetCategories()
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("select ID,CategoryName,MenuUrl from tblMenu where ParentID=0", con);
DataTable dt = new DataTable();
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
dt.Load(sdr);
}
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return dt;
}
public DataTable GetAllCategories()
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("select * from tblMenu", con);
DataTable dt = new DataTable();
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
dt.Load(sdr);
}
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return dt;
}
}
}