Click here to Skip to main content
11,706,455 members (49,109 online)
Click here to Skip to main content

Tagged as

Dynamically populating menu items from the database in ASP.NET

, 28 Mar 2012 CPOL 150.5K 10.3K 38
Rate this:
Please Sign up or sign in to vote.
Creating a Menu and populating items from a database dynamically after user login in ASP.NET.

Introduction

In this article, you will learn how to populate menu items dynamically from a database. The dynamicity of the Menu control is particularly useful when you need to restrict users from using some specific menu items.

Menu Table Structure in Database

 

Creating the Database Tables 

You will need a table in the database to store the MenuItems

Menu Table Structure in Database

Here, the MenuLocation column contains the URL of the target page when the corresponding menu item is clicked. 

Sample data(For Reference):  

Step 1: You need to specify some parent menu items with parentid = 0

Step 2: You can then assign submenuitems to these parents

For instance consider the following structure-

MenuID  MenuName    MenuLocation ParentID
  1	ParentItem1	NULL	   0
  2	ParentItem2	NULL	   0
  3	ParentItem3	NULL	   0
 11	SubMenuItem1	NULL	   1
 12	SubMenuItem2	NULL	   1
 21	SubMenuItem3	NULL	   2

Here ParentItem1,2,3 are Parents and SubMenuItem1,2,3 are their childs as specified.
 
As far as MenuLocation Column is conerned you can assign urls in it e.g Default.aspx or User/NewUserAdd.aspx etc. according to the directory structure of your application. 

MasterPage

Step 1: Place the ASP.NET Menu control in the MasterPage.

<div class="MenuBar">
	<asp:Menu ID="menuBar" runat="server" Orientation="Horizontal" Width="100%">
		<DynamicHoverStyle CssClass="DynamicHover" />
		<DynamicMenuItemStyle CssClass="DynamicMenuItem" />
		<DynamicSelectedStyle CssClass="DynamicHover" />

		<StaticHoverStyle CssClass="staticHover" />
		<StaticMenuItemStyle CssClass="StaticMenuItem" ItemSpacing="1px" />
		<StaticSelectedStyle CssClass="staticHover" />
	</asp:Menu>
</div>

MasterPage Code-Behind

Step 2: Using the following code, you are pulling the menu and submenu from the database in proper order. Call the following function from the Page_Load event of the MasterPage when !IsPostBack.

private void getMenu()
{
    Connect();
    con.Open();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string sql = "Select * from tbl_WebMenu";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(ds);
    dt = ds.Tables[0];
    DataRow[] drowpar = dt.Select("ParentID=" + 0);

    foreach (DataRow dr in drowpar)
    {
        menuBar.Items.Add(new MenuItem(dr["MenuName"].ToString(), 
                dr["MenuID"].ToString(), "", 
                dr["MenuLocation"].ToString()));
    }

    foreach (DataRow dr in dt.Select("ParentID >" + 0))
    {
        MenuItem mnu = new MenuItem(dr["MenuName"].ToString(), 
                       dr["MenuID"].ToString(), 
                       "", dr["MenuLocation"].ToString());
        menuBar.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu);
    }
    con.Close();
}

Points of Interest

This type of menu population is particularly useful when both the user count and menu item count are variable because you can set the access permissions (restricting users from accessing some pages at runtime) which will be described in the next article.

Note

The menu control may not work as  intended in some browsers such as chrome. So in that case you may have to add a '.browser' file under 'App_Browsers' folder of your project.

Open file safari.browser from Solution Explorer and remove all the code clear and add the following:

<browsers>
  <browser refID="safari1plus">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.Menu" adapterType="" />
    </controlAdapters>
  </browser>
</browsers>

Save the safari.browser file and there you go..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Prabhat Spark
Software Developer
India India
I'm a ASP.NET and C# programmer with interests in playing games such as Unreal Tournament, FIFA and listening to rap music that have meaningful lyrics...Smile | :)

You may also be interested in...

Comments and Discussions

 
QuestionDatabase Menu with Submenus Pin
Tanya Hammil4-Mar-15 2:55
memberTanya Hammil4-Mar-15 2:55 
QuestionCall the url in sql database when menubar navigates in asp.net Pin
Member 1110291624-Sep-14 22:47
memberMember 1110291624-Sep-14 22:47 
Questionhow can i add three level menu Pin
Muhamad Faizan Khan10-Mar-14 3:49
memberMuhamad Faizan Khan10-Mar-14 3:49 
AnswerRe: how can i add three level menu Pin
Prabhat Kr. Singh10-Mar-14 20:45
professionalPrabhat Kr. Singh10-Mar-14 20:45 
QuestionError Encountered Pin
huejiitech24-Feb-14 11:49
memberhuejiitech24-Feb-14 11:49 
AnswerRe: Error Encountered Pin
Prabhat Kr. Singh25-Feb-14 2:08
professionalPrabhat Kr. Singh25-Feb-14 2:08 
QuestionGives Following Error Pin
Member 1056418620-Feb-14 18:27
memberMember 1056418620-Feb-14 18:27 
GeneralNice Pin
Debopam Pal18-Nov-13 8:23
memberDebopam Pal18-Nov-13 8:23 
GeneralMy vote of 5 Pin
ho.kurd25-Aug-13 5:10
memberho.kurd25-Aug-13 5:10 
Questionmenu like tree view Pin
satyashri14321-Jul-13 4:32
membersatyashri14321-Jul-13 4:32 
QuestionHow can i create 5 submenu categories and some menu dont have? Pin
aaulhaq26-May-13 3:23
memberaaulhaq26-May-13 3:23 
AnswerRe: How can i create 5 submenu categories and some menu dont have? Pin
Prabhat Kr. Singh26-May-13 4:20
memberPrabhat Kr. Singh26-May-13 4:20 
GeneralRe: How can i create 5 submenu categories and some menu dont have? Pin
aaulhaq26-May-13 6:32
memberaaulhaq26-May-13 6:32 
Questionhow I can request url Pin
tanliem6535-May-13 21:55
membertanliem6535-May-13 21:55 
QuestionUp to which level we can create submenu Pin
nik kamble8-Apr-13 19:58
groupnik kamble8-Apr-13 19:58 
AnswerRe: Up to which level we can create submenu Pin
Prabhat Kr. Singh8-Apr-13 20:06
memberPrabhat Kr. Singh8-Apr-13 20:06 
SuggestionIf I click menus I want .aspx Page Pin
RekaRamalingam5-Feb-13 23:55
memberRekaRamalingam5-Feb-13 23:55 
QuestionAm I missing smth? "The name 'menuBar' does not exist in the current context" Pin
TieuTrung22-Jan-13 22:11
memberTieuTrung22-Jan-13 22:11 
AnswerRe: Am I missing smth? "The name 'menuBar' does not exist in the current context" Pin
Prabhat Kr. Singh8-Apr-13 20:08
memberPrabhat Kr. Singh8-Apr-13 20:08 
AnswerRe: Am I missing smth? "The name 'menuBar' does not exist in the current context" Pin
florenzdelima1-Dec-13 19:32
memberflorenzdelima1-Dec-13 19:32 
QuestionClick event to dynamically added menu item Pin
Kunjammu4-Oct-12 0:28
memberKunjammu4-Oct-12 0:28 
Questionmenu items from 2 tables? Pin
LOKImotive24-Sep-12 7:31
memberLOKImotive24-Sep-12 7:31 
SuggestionRe: menu items from 2 tables? Pin
Prabhat Kr. Singh24-Sep-12 19:50
memberPrabhat Kr. Singh24-Sep-12 19:50 
Questionmenu Pin
rimjhim_414-Sep-12 1:26
memberrimjhim_414-Sep-12 1:26 
SuggestionRe: menu Pin
Prabhat Kr. Singh14-Sep-12 6:05
memberPrabhat Kr. Singh14-Sep-12 6:05 
GeneralRe: menu Pin
rimjhim_419-Sep-12 23:51
memberrimjhim_419-Sep-12 23:51 
GeneralRe: menu Pin
rimjhim_420-Sep-12 0:44
memberrimjhim_420-Sep-12 0:44 
GeneralMy vote of 5 Pin
Karthik Reddy9-Sep-12 23:08
memberKarthik Reddy9-Sep-12 23:08 
QuestionExcellent and very simple way of adding dynamic menu Pin
vijaydanielj6-Sep-12 20:12
membervijaydanielj6-Sep-12 20:12 
GeneralMy vote of 5 Pin
vijaydanielj6-Sep-12 19:58
membervijaydanielj6-Sep-12 19:58 
Questionhow can display and add 4-th level of menu items ? Pin
kksingh_bittu21-Aug-12 1:29
memberkksingh_bittu21-Aug-12 1:29 
AnswerRe: how can display and add 4-th level of menu items ? Pin
Prabhat Kr. Singh21-Aug-12 1:39
memberPrabhat Kr. Singh21-Aug-12 1:39 
Generalhow to get submenus menu Pin
umapathi193-Aug-12 22:02
memberumapathi193-Aug-12 22:02 
AnswerRe: how to get submenus menu Pin
Prabhat Kr. Singh5-Aug-12 5:13
memberPrabhat Kr. Singh5-Aug-12 5:13 
QuestionHow do I use the MenuLocation data to call .aspx Pin
Rudy Razo29-Jun-12 9:12
memberRudy Razo29-Jun-12 9:12 
AnswerRe: How do I use the MenuLocation data to call .aspx Pin
Prabhat Kr. Singh29-Jun-12 19:07
memberPrabhat Kr. Singh29-Jun-12 19:07 
QuestionHow to generate submenu under submenu? Pin
biswarup8828-Jun-12 23:14
memberbiswarup8828-Jun-12 23:14 
AnswerRe: How to generate submenu under submenu? Pin
Prabhat Kr. Singh29-Jun-12 19:04
memberPrabhat Kr. Singh29-Jun-12 19:04 
QuestionMulti Level Hierarchy Pin
ernestmachado19-Jun-12 0:08
memberernestmachado19-Jun-12 0:08 
QuestionNeed help for dynamic Menu items Pin
madhupembarthi28-Mar-12 1:54
membermadhupembarthi28-Mar-12 1:54 
AnswerRe: Need help for dynamic Menu items Pin
Prabhat Kr. Singh28-Mar-12 2:16
memberPrabhat Kr. Singh28-Mar-12 2:16 
GeneralRe: Need help for dynamic Menu items Pin
madhupembarthi28-Mar-12 3:18
membermadhupembarthi28-Mar-12 3:18 
GeneralRe: Need help for dynamic Menu items Pin
Prabhat Kr. Singh28-Mar-12 5:35
memberPrabhat Kr. Singh28-Mar-12 5:35 
GeneralRe: Need help for dynamic Menu items Pin
Member 430475620-May-12 14:11
memberMember 430475620-May-12 14:11 
AnswerRe: Need help for dynamic Menu items Pin
Prabhat Kr. Singh20-May-12 19:17
memberPrabhat Kr. Singh20-May-12 19:17 
GeneralRe: Need help for dynamic Menu items Pin
ernestmachado19-Jun-12 1:01
memberernestmachado19-Jun-12 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 28 Mar 2012
Article Copyright 2012 by Prabhat Spark
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid