Friday, 2 August 2013

Create Dynamic Menu in EXT.NET





First create a menu table in the database like this

GO
/****** Object:  Table [dbo].[menu]    Script Date: 08/03/2013 00:03:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[menu](
[id] [int] IDENTITY(1,1) NOT NULL,
[rootmenu] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
[childmenu] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
[subchildmenu] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
 CONSTRAINT [PK_menu] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF



After that  create a page Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicMenu._Default" %>



<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>



<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
    <title>Dynamic Menu</title>
    <link href="/resources/css/examples.css" rel="stylesheet" />
</head>
<body>
    <ext:ResourceManager runat="server" />
   
   
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</body>
</html>




Then at code behind give the following code


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using System.Data.SqlClient;

namespace DynamicMenu
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet PrSet = PDataset("Select * from menu");

            foreach (DataRow dr in PrSet.Tables[0].Rows)
            {
                TreePanel tree = new TreePanel();

                tree.RootVisible = false;

                Ext.Net.Node root = new Ext.Net.Node();
                root.NodeID = dr["rootmenu"].ToString();
                root.Expanded = true;

                tree.Root.Add(root);

                Ext.Net.Node node1 = new Ext.Net.Node();

                node1.Text = dr["childmenu"].ToString();
                node1.Expanded = true;

                node1.Children.Add(new Ext.Net.Node()
                {
                    Text = dr["subchildmenu"].ToString(),
                    Href = "Default.aspx",
                    Icon = Icon.User,
                    Leaf = true

                });


                root.Children.Add(node1);

                Ext.Net.Node node2 = new Ext.Net.Node();
                node2.Text = dr["childmenu"].ToString();
                node2.Expanded = true;

                node2.Children.Add(new Ext.Net.Node()
                {
                    Text = dr["subchildmenu"].ToString(),
                    Icon = Icon.UserFemale,
                    Leaf = true
                });


                root.Children.Add(node2);



                Ext.Net.Panel window = new Ext.Net.Panel();
                window.Title = dr["RootMenu"].ToString();
                window.Width = Unit.Pixel(250);
                window.BodyBorder = 0;
                window.Layout = "Accordion";
                window.Collapsible = true;
                window.Items.Add(tree);

                this.PlaceHolder1.Controls.Add(window);
            }
        }

        protected DataSet PDataset(string Select_Statement)
        {

            SqlConnection SqlCon = new SqlConnection("Your Connectionstring");

            SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);

            DataSet ds = new DataSet();

            ad.Fill(ds);

            return ds;



        }
    }
}



1 comment: