Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<form id="form1" runat="server">
<div>
<ul id="menu"></ul>
</div>
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" />
<script type="text/javascript">
$(function () {
$('ul').menu();
$.ajax({
url: "Handler.ashx",
method: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
$.each(data, function () {
var li = $('<li value="' + this.Id + '">' + this.MenuText + '</li>');
if (!this.Active) {
li.addClass('ui-state-disabled');
}
li.appendTo($('#menu'));
});
}
});
$('body').on('mouseenter', 'ul li', function () {
var menu = $(this);
$.ajax({
url: "Handler.ashx?ParentId=" + $(this).attr('value'),
method: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var ul = $('<ul></ul>').empty();
$.each(data, function () {
var li = $('<li value="' + this.Id + '">' + this.MenuText + '</li>');
if (!this.Active) {
li.addClass('ui-state-disabled');
}
li.appendTo($(ul));
});
$(ul).appendTo($(menu));
}
});
});
$('body').on('mouseleave', 'ul li', function () {
$(this).find('ul').remove();
});
});
</script>
Generic Handler
C#
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = 0;
int.TryParse(context.Request.QueryString["ParentId"], out id);
List<Menu> menus = GetMenus(id);
JavaScriptSerializer se = new JavaScriptSerializer();
context.Response.ContentType = "text/plain";
context.Response.Write(se.Serialize(menus));
}
public static List<Menu> GetMenus(int parent)
{
List<Menu> menus = new List<Menu>();
DataRow[] drs = GetData().Select("ParentId=" + parent);
foreach (DataRow dr in drs)
{
Menu menu = new Menu();
menu.Id = Convert.ToInt32(dr["Id"]);
menu.MenuText = dr["MenuText"].ToString();
menu.ParentId = Convert.ToInt32(dr["ParentId"]);
menu.Active = Convert.ToBoolean(dr["Active"]);
menus.Add(menu);
}
return menus;
}
public static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Id",typeof(int)),
new DataColumn("MenuText",typeof(string)),
new DataColumn("ParentId",typeof(int)),
new DataColumn("Active",typeof(bool)) });
dt.Rows.Add(1, "USA", 0, 1);
dt.Rows.Add(2, "India", 0, 1);
dt.Rows.Add(3, "Australia", 0, 1);
dt.Rows.Add(5, "Virginia", 1, 1);
dt.Rows.Add(6, "Maryland", 1, 1);
dt.Rows.Add(7, "AP", 2, 1);
dt.Rows.Add(8, "MP", 2, 1);
dt.Rows.Add(9, "Karnataka", 2, 1);
dt.Rows.Add(10, "Bangalore", 9, 1);
dt.Rows.Add(11, "Mangalore", 9, 1);
dt.Rows.Add(12, "Mysore", 9, 0);
return dt;
}
public class Menu
{
public int Id { get; set; }
public string MenuText { get; set; }
public int ParentId { get; set; }
public bool Active { get; set; }
}
public bool IsReusable
{
get
{
return false;
}
}
}
VB.Net
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.Collections.Generic
Imports System.Data
Imports System.Web.Script.Serialization
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim id As Integer = 0
Integer.TryParse(context.Request.QueryString("ParentId"), id)
Dim menus As List(Of Menu) = GetMenus(id)
Dim se As JavaScriptSerializer = New JavaScriptSerializer()
context.Response.ContentType = "text/plain"
context.Response.Write(se.Serialize(menus))
End Sub
Public Shared Function GetMenus(ByVal parent As Integer) As List(Of Menu)
Dim menus As List(Of Menu) = New List(Of Menu)()
Dim drs As DataRow() = GetData().[Select]("ParentId=" & parent)
For Each dr As DataRow In drs
Dim menu As Menu = New Menu()
menu.Id = Convert.ToInt32(dr("Id"))
menu.MenuText = dr("MenuText").ToString()
menu.ParentId = Convert.ToInt32(dr("ParentId"))
menu.Active = Convert.ToBoolean(dr("Active"))
menus.Add(menu)
Next
Return menus
End Function
Public Shared Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {
New DataColumn("Id", GetType(Integer)),
New DataColumn("MenuText", GetType(String)),
New DataColumn("ParentId", GetType(Integer)),
New DataColumn("Active", GetType(Boolean))})
dt.Rows.Add(1, "USA", 0, 1)
dt.Rows.Add(2, "India", 0, 1)
dt.Rows.Add(3, "Australia", 0, 1)
dt.Rows.Add(5, "Virginia", 1, 1)
dt.Rows.Add(6, "Maryland", 1, 1)
dt.Rows.Add(7, "AP", 2, 1)
dt.Rows.Add(8, "MP", 2, 1)
dt.Rows.Add(9, "Karnataka", 2, 1)
dt.Rows.Add(10, "Bangalore", 9, 1)
dt.Rows.Add(11, "Mangalore", 9, 1)
dt.Rows.Add(12, "Mysore", 9, 0)
Return dt
End Function
Public Class Menu
Public Property Id As Integer
Public Property MenuText As String
Public Property ParentId As Integer
Public Property Active As Boolean
End Class
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class