ASP.NET Menu Control (CSASPNETMenu)


The sample demonstrates how to bind the ASP.NET Menu Control to the Database. All the contents of the Menu control are generated dynamically, if we want to add some new navigation items into the website, we only need to insert some data to the database instead of modifying the source code.

Running the Sample

Please follow these demonstration steps below.

Step 1: Open the CSASPNETMenu.sln. Expand the CSASPNETMenu web application and press Ctrl + F5 to show the CSASPNETMenu.aspx.

Step 2: We will see a Menu control on the page, you can click the Menu items and jump to the Test.aspx page.


Step 3: If you want to change the url string variables to your website��s pages, you need to modify the DataTable object in the code-bebind page (CSASPNETMenu.aspx.cs). Perhaps we need a table in DataBase for managing menu items content and navigator url, you can also create a database file to constitute a DataTable like this sample. 

Step 4: Validation finished.

Using the Code

Code Logical:

Step 1. Create a C# "ASP.NET Empty Web Application" in Visual Studio 2008. Name it as " CSASPNETMenu ". The project includes two web form pages for demonstrating how to bind Menu control with a DataTable variable.

Step 2. Add two web form pages in the root directory of the application, name them as ��CSASPNETMenu.aspx��, ��Test.aspx��. The CSASPNETMenu page is used to show Menu control and redirect to the Test page by clicking the items of Menu.

Step 3. Drag and Drop a Menu control to the CSASPNETMenu page, you can set the styles of the Menu to make it looks better. Then you can begin to write C# code for generating DataTable variable and bind it to the Menu control, in this sample, we create a GetData() method to generate data.

The following code is used to generate a DataTable variable and fill it with some default value:     

public DataSet GetData()
    // In order to test, we use the memory tables as the datasource.
    DataTable mainTB = new DataTable();
    DataColumn mainIdCol = new DataColumn("mainId");
    DataColumn mainNameCol = new DataColumn("mainName");
    DataColumn mainUrlCol = new DataColumn("mainUrl");

    DataTable childTB = new DataTable();
    DataColumn childIdCol = new DataColumn("childId");
    DataColumn childNameCol = new DataColumn("childName");

    // The MainId column of the child table is the foreign key to the main table.
    DataColumn childMainIdCol = new DataColumn("MainId");         
    DataColumn childUrlCol = new DataColumn("childUrl");


    // Insert some test records to the main table.
    DataRow dr = mainTB.NewRow();
    dr[0] = "1";
    dr[1] = "Home";
    dr[2] = "Test.aspx";
    DataRow dr1 = mainTB.NewRow();
    dr1[0] = "2";
    dr1[1] = "Articles";
    dr1[2] = "Test.aspx";
    DataRow dr2 = mainTB.NewRow();
    dr2[0] = "3";
    dr2[1] = "Help";
    dr2[2] = "Test.aspx";
    DataRow dr3 = mainTB.NewRow();
    dr3[0] = "4";
    dr3[1] = "DownLoad";
    dr3[2] = "Test.aspx";

    // Insert some test records to the child table
    DataRow dr5 = childTB.NewRow();
    dr5[0] = "1";
    dr5[1] = "ASP.NET";
    dr5[2] = "2";
    dr5[3] = "Test.aspx";
    DataRow dr6 = childTB.NewRow();
    dr6[0] = "2";
    dr6[1] = "SQL Server";
    dr6[2] = "2";
    dr6[3] = "Test.aspx";
    DataRow dr7 = childTB.NewRow();
    dr7[0] = "3";
    dr7[1] = "JavaScript";
    dr7[2] = "2";
    dr7[3] = "Test.aspx";

    // Use the DataSet to contain that two tables.
    DataSet ds = new DataSet();          

    // Build the relation between the main table and the child table.
    ds.Relations.Add("Child", ds.Tables[0].Columns["mainId"], ds.Tables[1].Columns["MainId"]);

    return ds;



Step 4. Then we need to create a GenerateMenuItem method for binding DataTable to the Menu control, and we need only execute this method when CSASPMETMenu page is loaded for the first  time.

The following code is used to bind DataTable to the Menu control when page is loaded for the first time.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)

public void GenerateMenuItem()
    // Get the data from database.
    DataSet ds = GetData();

    foreach (DataRow mainRow in ds.Tables[0].Rows)
        // Load the records from the main table to the menu control.
        MenuItem masterItem = new MenuItem(mainRow["mainName"].ToString());
        masterItem.NavigateUrl = mainRow["mainUrl"].ToString();

        foreach (DataRow childRow in mainRow.GetChildRows("Child"))
            // According to the relation of the main table and the child table, load the data from the child table.
            MenuItem childItem = new MenuItem((string)childRow["childName"]);
            childItem.NavigateUrl = childRow["childUrl"].ToString();



Step 5. Build the application and you can debug it.


More Information

��         ASP.NET Menu Control Overview