(jQuery Vertical Mega Menu in Asp.net) – Đối với các Website thương mại điện tử, do số lượng các ngành hàng nhiều và có nhiều cấp nên việc phân loại ngành hàng càng đơn giản, thuận tiện càng dễ cho người mua hàng thì hàng hóa sẽ bán được nhiều hơn. 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 jQuery để tạo Vertical Menu. Menu gồm 3 cấp, các thông tin đượ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
- B5: Tạo Project trong Microsoft Visual Studio 2010
End Namespace
- 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- 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
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@strSQLC# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace jQueryVerticalMegaMenu
{
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 jQueryVerticalMegaMenu
Public Class SqlDataProvider
#Region "Membres Prives"
Shared _IsError As Boolean = False
Private _connectionString As String
#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() As String
Get
Return _connectionString
End Get
End Property
#End Region
#Region "Functions"
Public Function FillTable(ByVal sql As String) As DataTable
Try
Dim tb As New DataTable
Dim adap As New SqlDataAdapter(sql, _connectionString)
adap.Fill(tb)
Return tb
Catch ex As Exception
Return Nothing
End Try
End Function
#End Region
End Class
- B6: Mở file Site.Master dạng HTML và bổ xung đoạn mã phía dưới trong thẻ Head
<head id="Head1" runat="server">
<title>jQuery Vertical Mega Menu in Asp.net</title>
<link href="Styles/Site.css"rel="stylesheet"type="text/css"/>
<link href="Styles/vertical_menu.css"rel="stylesheet"type="text/css"/>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type='text/javascript'src='Js/jquery.hoverIntent.minified.js'></script>
<script type='text/javascript'src='Js/jquery.dcverticalmegamenu.1.1.js'></script>
<asp:ContentPlaceHolderID="HeadContent"runat="server">
</asp:ContentPlaceHolder>
</head>
<%@ PageTitle="jQuery Vertical Mega Menu in Asp.net" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jQueryVerticalMegaMenu._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h3>
jQuery Vertical Mega Menu in Asp.net
</h3>
<script type="text/javascript">
$(document).ready(function ($) {
$('#mega-1').dcVerticalMegaMenu({
rowItems: '3',
speed: 'fast',
effect: 'slide',
direction: 'right'
});
});
</script>
<div class="demo-container clear">
<div class="dcjq-vertical-mega-menu">
<ul id="mega-1" class="menu">
<asp:Repeater ID="rptMenu"OnItemDataBound="rptMenu_ItemDataBound"runat="server">
<ItemTemplate>
<asp:Literal ID="ltMenu" runat="server"></asp:Literal>
<li id='<%# Eval("Css")%>'>
<a href="#"><%# Eval("MenuName")%></a>
<ul>
<asp:Repeater ID="rptMenu1"OnItemDataBound="Detail_Bound"runat="server">
<ItemTemplate>
<li id='<%# Eval("Css")%>'>
<a href="#"><%# Eval("MenuName")%></a>
<ul>
<asp:Repeater ID="rptMenu2"runat="server">
<ItemTemplate>
<li id='<%# Eval("Css")%>'>
<a href="#"><%# Eval("MenuName")%></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</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.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace jQueryVerticalMegaMenu
{
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"Event Handles"
#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)
{
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();
}
}
}
#endregion
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 jQueryVerticalMegaMenu
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))
Return objBind
End Function
#End Region
#Region "Repeater Methods"
Private SubrptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) HandlesrptMenu.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
End Namespace
Bây giờ chạy Project bạn sẽ có kết quả như ảnh phía dưới.
Chúc các bạn thành công!
Quang Bình
0 comments Blogger 0 Facebook
Post a Comment