Hi ishi_12987,
Refer below code.
HTML
<form id="form1" runat="server">
<div class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
aria-expanded="false">
<span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span><span
class="icon-bar"></span><span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ASPSnippets</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" RenderingMode="List"
IncludeStyleBlock="false" StaticMenuStyle-CssClass="nav navbar-nav" DynamicMenuStyle-CssClass="dropdown-menu">
</asp:Menu>
</div>
</div>
</div>
<script type="text/javascript">
Sys.WebForms.Menu._elementObjectMapper.getMappedObject = function () {
return false;
};
$(function () {
$(".navbar-nav li, .navbar-nav a, .navbar-nav ul").removeAttr('style');
$(".dropdown-menu").closest("li").removeClass().addClass("dropdown-toggle");
$(".dropdown-toggle").find("a[href='javascript:;']").attr("data-toggle", "dropdown");
$(".dropdown-toggle").find("a[href='javascript:;'].level1").append("<span class='caret'></span>");
$(".dropdown-toggle").find("a[href='javascript:;']:not(.level1)").closest('li').addClass('dropdown-submenu');
$("a.selected").closest("li").addClass("active");
$("a.selected").closest(".dropdown-toggle").addClass("active");
$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function (event) {
event.preventDefault();
event.stopPropagation();
$(this).parent().siblings().removeClass('open');
$(this).parent().toggleClass('open');
});
});
</script>
<hr />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData(0);
PopulateMenu(dt, 0, null);
}
}
private DataTable GetData(int parentMenuId)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("ParentMenuId"),
new DataColumn("MenuId"),
new DataColumn("Title"),
new DataColumn("Url") });
dt.Rows.Add(0, 1, "Home", "~/Home.aspx");
dt.Rows.Add(0, 2, "Master", "javascript:;");
dt.Rows.Add(0, 3, "Transaction", "javascript:;");
dt.Rows.Add(0, 4, "Reports", "javascript:;");
dt.Rows.Add(2, 5, "Common", "javascript:;");
dt.Rows.Add(3, 6, "Common", "javascript:;");
dt.Rows.Add(3, 7, "Menu2", "javascript:;");
dt.Rows.Add(3, 8, "Menu10", "javascript:;");
dt.Rows.Add(4, 9, "Common1", "javascript:;");
dt.Rows.Add(4, 10, "Common2", "javascript:;");
dt.Rows.Add(5, 11, "County", "~/CountryMaster.aspx");
dt.Rows.Add(5, 12, "City", "~/CityMaster.aspx");
dt.Rows.Add(5, 13, "State", "~/StateMaster.aspx");
dt.Rows.Add(6, 14, "Form1", "~/Form1.aspx");
dt.Rows.Add(6, 15, "Form2", "~/Form2.aspx");
dt.Rows.Add(7, 16, "Form3", "~/Form3.aspx");
dt.Rows.Add(7, 17, "Form4", "~/Form4.aspx");
dt.Rows.Add(8, 18, "Form10", "~/Form10.aspx");
dt.Rows.Add(9, 19, "Report1", "~/Report4.aspx");
dt.Rows.Add(9, 20, "Report2", "~/Report2.aspx");
DataTable dtFinal = dt.Clone();
DataRow[] dr = dt.Select("ParentMenuId=" + parentMenuId);
if (dr.Length > 0)
{
dtFinal = dr.CopyToDataTable();
}
return dtFinal;
}
private void PopulateMenu(DataTable dt, int parentMenuId, MenuItem parentMenuItem)
{
string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
foreach (DataRow row in dt.Rows)
{
MenuItem menuItem = new MenuItem
{
Value = row["MenuId"].ToString(),
Text = row["Title"].ToString(),
NavigateUrl = row["Url"].ToString(),
Selected = row["Url"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
};
if (parentMenuId == 0)
{
Menu1.Items.Add(menuItem);
DataTable dtChild = this.GetData(int.Parse(menuItem.Value));
PopulateMenu(dtChild, int.Parse(menuItem.Value), menuItem);
}
else
{
parentMenuItem.ChildItems.Add(menuItem);
if (parentMenuId > 0)
{
DataTable dtChild = this.GetData(int.Parse(menuItem.Value));
PopulateMenu(dtChild, int.Parse(menuItem.Value), menuItem);
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = Me.GetData(0)
PopulateMenu(dt, 0, Nothing)
End If
End Sub
Private Function GetData(parentMenuId As Integer) As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {
New DataColumn("ParentMenuId"),
New DataColumn("MenuId"),
New DataColumn("Title"),
New DataColumn("Url")})
dt.Rows.Add(0, 1, "Home", "~/Home.aspx")
dt.Rows.Add(0, 2, "Master", "javascript:;")
dt.Rows.Add(0, 3, "Transaction", "javascript:;")
dt.Rows.Add(0, 4, "Reports", "javascript:;")
dt.Rows.Add(2, 5, "Common", "javascript:;")
dt.Rows.Add(3, 6, "Common", "javascript:;")
dt.Rows.Add(3, 7, "Menu2", "javascript:;")
dt.Rows.Add(3, 8, "Menu10", "javascript:;")
dt.Rows.Add(4, 9, "Common1", "javascript:;")
dt.Rows.Add(4, 10, "Common2", "javascript:;")
dt.Rows.Add(5, 11, "County", "~/CountryMaster.aspx")
dt.Rows.Add(5, 12, "City", "~/CityMaster.aspx")
dt.Rows.Add(5, 13, "State", "~/StateMaster.aspx")
dt.Rows.Add(6, 14, "Form1", "~/Form1.aspx")
dt.Rows.Add(6, 15, "Form2", "~/Form2.aspx")
dt.Rows.Add(7, 16, "Form3", "~/Form3.aspx")
dt.Rows.Add(7, 17, "Form4", "~/Form4.aspx")
dt.Rows.Add(8, 18, "Form10", "~/Form10.aspx")
dt.Rows.Add(9, 19, "Report1", "~/Report4.aspx")
dt.Rows.Add(9, 20, "Report2", "~/Report2.aspx")
Dim dtFinal As DataTable = dt.Clone()
Dim dr As DataRow() = dt.Select("ParentMenuId=" & parentMenuId)
If dr.Length > 0 Then
dtFinal = dr.CopyToDataTable()
End If
Return dtFinal
End Function
Private Sub PopulateMenu(dt As DataTable, parentMenuId As Integer, parentMenuItem As MenuItem)
Dim currentPage As String = Path.GetFileName(Request.Url.AbsolutePath)
For Each row As DataRow In dt.Rows
Dim menuItem As New MenuItem() With { _
.Value = row("MenuId").ToString(), _
.Text = row("Title").ToString(), _
.NavigateUrl = row("Url").ToString(), _
.Selected = row("Url").ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase) _
}
If parentMenuId = 0 Then
Menu1.Items.Add(menuItem)
Dim dtChild As DataTable = Me.GetData(Integer.Parse(menuItem.Value))
PopulateMenu(dtChild, Integer.Parse(menuItem.Value), menuItem)
Else
parentMenuItem.ChildItems.Add(menuItem)
If parentMenuId > 0 Then
Dim dtChild As DataTable = Me.GetData(Integer.Parse(menuItem.Value))
PopulateMenu(dtChild, Integer.Parse(menuItem.Value), menuItem)
End If
End If
Next
End Sub
Screenshot