Home / ASP.NET Wiki / HTML / Web Controls / Navigation controls / menu

menu

 Rate It (2)

Sample code to create menu dynamically: 

MenuTest.aspx:

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
        </asp:Menu>

MenuTest.asp.cs:

static string constr = "SQLOLEDB;Data Source=ABC;Integrated Security=SSPI;Initial Catalog=XYZ";
    OleDbConnection cn = new OleDbConnection(constr);

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            PopulateRootLevel();
    }

 

private void PopulateRootLevel()
    {
        OleDbCommand objCommand = new OleDbCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id)               childnodecount FROM SampleCategories sc where parentID IS NULL", cn);
        OleDbDataAdapter da = new OleDbDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, Menu1.Items);
    }

 

private void PopulateSubLevel(int parentid, MenuItem parentMenu)
    {
        OleDbCommand objCommand = new OleDbCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID="+parentid+"", cn);
       OleDbDataAdapter da = new OleDbDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, parentMenu.ChildItems);
    }

 

private void PopulateNodes(DataTable dt, MenuItemCollection items)
    {
        foreach (DataRow dr in dt.Rows)
        {
            MenuItem mi = new MenuItem();
            mi.Text = dr["title"].ToString();
            mi.Value = dr["id"].ToString();
            items.Add(mi);

            //If node has child nodes, then enable on-demand populating
           
            bool flag = ((int)(dr["childnodecount"]) > 0);

            if (flag)
            {
                menuCreate(mi);
            }
        }
    }

    private void menuCreate(MenuItem m)
    {
        MenuEventArgs e = new MenuEventArgs(m);
        PopulateSubLevel(Int32.Parse(e.Item.Value), e.Item);
    }
}

 

Here is the database script

For schema generating

 CREATE TABLE [dbo].[SAMPLECATEGORIES](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [parentid] [int] NULL,
    [title] [nvarchar](255) COLLATE SQL_SwedishStd_Pref_CP1_CI_AS NOT NULL
)

 

For testing, data samples

INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (1, NULL, N'Category 1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (2, NULL, N'Category 2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (3, NULL, N'Category 3')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (4, 1, N'Category 1.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (5, 1, N'Category 1.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (6, 2, N'Category 2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (7, 2, N'Category 2.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (8, 2, N'Category 2.3')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (9, 7, N'Category 2.2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (10, 7, N'Category 2.2.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (11, 10, N'Category 2.2.2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (12, 6, N'Category 2.1.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (13, 3, N'Category 3.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (14, 7, N'Category 2.2.3')

 

Revision number 2, Friday, February 22, 2008 11:42:27 AM by XIII

Comments

how to make menu items visible/invisible??

Hi, I would like to show an image in a div on the same page when clicking on a certain menuitem. How can I access the click(-function) ??

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)