(jQuery Mega Menu Navigation bar with bootstrap in Asp.net) – Khi Menu trong Website của bạn có nhiều cấp, đặc biệt là Website thương mại điện tử nếu không được sắp xếp hiển thị khoa học sẽ gây khó khăn cho người sử dụng. Bài viết dưới đây sẽ hướng dẫn các bạn cách sử dụng Control Repeater và thư viện bootstrap để tạo Mega Menu Navigation bar. Menu gồm 3 cấp, các thông tin như: Tên Menu, đường dẫn, Css được lấy từ CSDL SQL Server.
- B1: Tạo CSDL DynamicallyMenu trong SQL Server
- B2: Tạo Bảng MenuItems có cấu trúc phía dưới trong CSDL SQL Server.
- B3: Nhập dữ liệu cho bảng MenuItems
- B4: Tạo stored procedure trong SQL Server
- B5: Tạo Project trong Microsoft Visual Studio 2010
- B3: Nhập dữ liệu cho bảng MenuItems
- B4: Tạo stored procedure trong SQL Server
CREATE Procedure[dbo].[Pro_Menu_List]
@ParentID int,
@IsVisible bit
as
declare@strSQL nvarchar(1000)
declare @strWhere nvarchar(500)
declare @strOrder nvarchar (50)
set @strSQL= 'Select * from MenuItems'
set @strWhere =' where 1=1'
if @IsVisible=0
set @strWhere= @strWhere +' and (IsVisible=0 Or IsVisible Is Null)'
if @ParentID<>-1 And@ParentID<>0
set @strWhere= @strWhere +' and ParentID='+ convert(nvarchar,@ParentID)
if @ParentID=-1
set @strWhere= @strWhere +' and (ParentID=-1 Or ParentID Is Null)'
set @strOrder =' Order by MenuOrder, MenuName'
set @strSQL=@strSQL+@strWhere+@strOrder
print @strSQL
exec sp_executesql@strSQLTrong Visual Studio tạo 1 Class có tên: Utility và nhập đoạn Code phía dưới cho Class này.
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace MegaMenuNavigationBar
{
public class SqlDataProvider
{
#region"Membres Prives"
private string_connectionString;
#endregion
#region"Constructeurs"
public SqlDataProvider()
{
try
{
_connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
}
catch
{
}
}
#endregion
#region"Proprietes"
public stringConnectionString
{
get { return_connectionString; }
}
#endregion
#region"Functions"
public DataTableFillTable(string ProcName, params ObjectPara[] Para)
{
try
{
DataTable tb = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter(ProcName, _connectionString);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
if (Para != null)
{
foreach (ObjectParap in Para)
{
adap.SelectCommand.Parameters.Add(new SqlParameter(p.Name, p.Value));
}
}
adap.Fill(tb);
return tb;
}
catch
{
return null;
}
}
#endregion
}
public class ObjectPara
{
string _name;
object _Value;
public ObjectPara(stringPname, object PValue)
{
_name = Pname;
_Value = PValue;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public object Value
{
get { return _Value; }
set { _Value = value; }
}
}
}VB.NET Code
Imports System.Data.SqlClient
Imports System.Data
Namespace MegaMenuNavigationBar
Public Class SqlDataProvider
#Region "Membres Prives"
Shared _IsError As Boolean = False
Private _connectionString AsString
#End Region
#Region "Constructeurs"
Public Sub New()
Try
_connectionString = ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString
_IsError = False
Catch ex As Exception
_IsError = True
End Try
End Sub
#End Region
#Region "Proprietes"
Public ReadOnly Property ConnectionString() AsString
Get
Return _connectionString
End Get
End Property
#End Region
#Region "Functions"
Public FunctionFillTable(ByVal ProcName As String, ByVal ParamArrayPara() As ObjectPara) As DataTable
Try
Dim tb AsNew DataTable
Dim adap AsNew SqlDataAdapter(ProcName, _connectionString)
adap.SelectCommand.CommandType = CommandType.StoredProcedure
If NotPara Is NothingThen
For Eachp As ObjectParaIn Para
adap.SelectCommand.Parameters.Add(New SqlParameter(p.Name, p.Value))
Next
End If
adap.Fill(tb)
Return tb
Catch ex As Exception
Return Nothing
End Try
End Function
#End Region
End Class
Public Class ObjectPara
Dim _name As String
Dim _Value As Object
Sub New(ByVal Pname As String, ByVal PValue As Object)
_name = Pname
_Value = PValue
End Sub
Public PropertyName() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public PropertyValue() As Object
Get
Return _Value
End Get
Set(ByVal value As Object)
_Value = value
End Set
End Property
End Class
End Namespace
Chú ý: Thuộc tính SiteSqlServer chính là chuỗi Connect với SQL Server trong file Web.Config
- B6: Mở file Site.Master dạng HTML và bổ xung đoạn mã phía dưới trong thẻ Head
- B7: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
<head id="Head1" runat="server">
<title>How to create mega menu navigation bar with twitter bootstrap 3.0</title>
<link href="Styles/Site.css"rel="stylesheet"type="text/css"/>
<link rel="stylesheet"type="text/css"href="Styles/bootstrap.css"/>
<script type="text/javascript"src="Js/jquery.js"></script>
<script type="text/javascript"src="Js/bootstrap.js"></script>
<asp:ContentPlaceHolderID="HeadContent"runat="server">
</asp:ContentPlaceHolder>
</head>
- B7: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
<%@ PageTitle="How to create mega menu navigation bar with twitter bootstrap in Asp.net" Language="C#"MasterPageFile="~/Site.master"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="MegaMenuNavigationBar._Default"%>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h3>
How to create mega menu navigation bar with twitter bootstrap 3.0 in Asp.net
</h3>
<div class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<asp:Repeater ID="rptMenu" OnItemDataBound="rptMenu_ItemDataBound" runat="server">
<ItemTemplate>
<li class="dropdown menu-large">
<a href='<%# Eval("URL")%>' class="dropdown-toggle"data-toggle="dropdown"><%# Eval("MenuName")%><b class="caret"></b></a>
<ul class="dropdown-menu megamenu row">
<asp:Repeater ID="rptMenu1"OnItemDataBound="Detail_Bound"runat="server">
<ItemTemplate>
<li class="col-sm-4">
<ul>
<li><b><a href="#"><%# Eval("MenuName")%></a></b></li>
<asp:Repeater ID="rptMenu2"runat="server">
<ItemTemplate>
<li><a href="#"><%# Eval("MenuName")%></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</div>
</div>
</asp:Content>- B8: Viết Code cho file Default.aspx
C# Code
//Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MegaMenuNavigationBar
{
public partial class _Default : System.Web.UI.Page
{
#region"Create Menu"
private voidCreateMenu()
{
DataTable objBind = newDataTable();
objBind = BindData(-1);
if (objBind != null)
{
if (objBind.Rows.Count > 0)
{
rptMenu.DataSource = objBind;
rptMenu.DataBind();
}
}
}
#endregion
#region"Bind Data"
private DataTableBindData(int ParentID)
{
SqlDataProvider objSQL = newSqlDataProvider();
DataTable objBind = objSQL.FillTable("Pro_Menu_List", new ObjectPara("@ParentID", ParentID), new ObjectPara("@IsVisible", 1));
return objBind;
}
#endregion
#region"Repeater Methods"
protected voidrptMenu_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgse)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
int MenuID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "MenuID"));
string MenuName = DataBinder.Eval(e.Item.DataItem, "MenuName").ToString();
DataTable objBind = new DataTable();
objBind = BindData(MenuID);
Repeater rptMenu = (Repeater)e.Item.FindControl("rptMenu1");
if (rptMenu != null)
{
rptMenu.DataSource = objBind;
rptMenu.DataBind();
}
}
}
protected voidDetail_Bound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
int MenuID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "MenuID"));
string MenuName = DataBinder.Eval(e.Item.DataItem, "MenuName").ToString();
DataTable objBind = new DataTable();
objBind = BindData(MenuID);
Repeater rptMenu = (Repeater)e.Item.FindControl("rptMenu2");
if (rptMenu != null)
{
rptMenu.DataSource = objBind;
rptMenu.DataBind();
}
}
}
catch
{
}
}
#endregion
#region"Event Handles"
protected voidPage_Load(object sender, System.EventArgs e)
{
try
{
if (!IsPostBack)
{
CreateMenu();
}
}
catch
{
}
}
#endregion
}
}VB.NET Code
'Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
Namespace MegaMenuNavigationBar
Public Class _Default
Inherits System.Web.UI.Page
#Region "Create Menu"
Private SubCreateMenu()
Dim objBind As New DataTable
objBind = BindData(-1)
If Not objBind Is Nothing Then
If objBind.Rows.Count > 0 Then
rptMenu.DataSource = objBind
rptMenu.DataBind()
End If
End If
End Sub
#End Region
#Region "Bind Data"
Private FunctionBindData(ByVal ParentID As Integer) As DataTable
Dim objSQL As New SqlDataProvider
Dim objBind As DataTable = objSQL.FillTable("Pro_Menu_List", New ObjectPara("@ParentID", ParentID), New ObjectPara("@IsVisible", 1))
ReturnobjBind
End Function
#End Region
#Region "Repeater Methods"
Private SubrptMenu_ItemDataBound(ByVal sender As Object, ByVal e AsSystem.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim MenuID AsInteger = DataBinder.Eval(e.Item.DataItem, "MenuID")
Dim MenuName AsString = DataBinder.Eval(e.Item.DataItem, "MenuName")
Dim objBind AsNew DataTable
objBind = BindData(MenuID)
Dim rptMenu1 AsRepeater = DirectCast(e.Item.FindControl("rptMenu1"), Repeater)
If NotrptMenu1 Is NothingThen
rptMenu1.DataSource = objBind
rptMenu1.DataBind()
End If
End If
End Sub
Public SubDetail_Bound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
Try
If (e.Item.ItemType = ListItemType.Item Ore.Item.ItemType = ListItemType.AlternatingItem) Then
Dim MenuID AsInteger = DataBinder.Eval(e.Item.DataItem, "MenuID")
Dim MenuName AsString = DataBinder.Eval(e.Item.DataItem, "MenuName")
Dim objBind AsNew DataTable
objBind = BindData(MenuID)
Dim rptMenu AsRepeater = DirectCast(e.Item.FindControl("rptMenu2"), Repeater)
If NotrptMenu Is NothingThen
rptMenu.DataSource = objBind
rptMenu.DataBind()
End If
End If
Catch exception As Exception
End Try
End Sub
#End Region
#Region "Event Handles"
Protected SubPage_Load(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Me.Load
Try
If Page.IsPostBack = False Then
CreateMenu()
End If
Catch ex As Exception
End Try
End Sub
#End Region
End Class
Quang Bình
0 comments Blogger 0 Facebook
Post a Comment