Beruflich Dokumente
Kultur Dokumente
Home
Articles
Quick Answers
Discussions
Learning Zones
Features
Help!
4
The Lounge
Sea ch i e
Web Development
ASP.NET C ontrols
Menu C ontrols Licence First Posted Views Downloads Bookmarked C POL 26 Mar 2012 2,891 60 5 times
See Also
More like this More by this author
Creating a Menu and populating items from a database dynamically after user login in ASP.NET.
Tip Browse Code Stats Revisions (9) Alternatives
5.00 (1 vote) Hot News: 10 Best Practices of C ode C ommenting & Formatting The C ode Project Insider. Free each morning.
Download source - 11 KB
Introduction
In this article, you will learn how to populate menu items dynamically from a database. The dynamicity of the M n control is particularly useful when you need to restrict users from eu using some specific menu items.
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 structureC ollapse | C opy C ode
Related Articles
Dynamic Menu Based on Database Populating Menu C ontrol in ASP.NET 2.0 - using different data sources ASP.NET Dynamic XML Web Menu C ontrol based on JavaScript Dynamic Items Populate TreeView Menu with XML Dynamically changing menu items according to C ultureInfo
MnI euD 1 2 3 1 1 1 2
MnNm euae MnLcto PrnI euoain aetD PrnIe1 aettm NL UL 0 PrnIe2 aettm NL UL 0 PrnIe3 aettm NL UL 0 Sbeutm uMnIe1 NL UL 1 Sbeutm uMnIe2 NL UL 1
www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas.aspx
1/3
29/03/12
2 1
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 D f u t a p or eal.sx U e / e U e A d a p etc. according to the directory structure of your application. srNwsrd.sx
MasterPage
Step 1: Place the ASP.NET M n control in the MasterPage. eu
<i cas"eua" dv ls=MnBr> <s:euI=mnBr rnt"evr Oinain"oiotl Wdh"0%> apMn D"eua" ua=sre" retto=Hrzna" it=10" <yaiHvrtl Csls=Dnmcoe"/ DnmcoeSye sCas"yaiHvr > <yaiMnIeSyeCsls=Dnmceutm / Dnmceutmtl sCas"yaiMnIe" > <yaiSlceSyeCsls=Dnmcoe"/ Dnmceetdtl sCas"yaiHvr > <ttcoeSyeCsls=saiHvr / SaiHvrtl sCas"ttcoe" > <ttceutmtl Csls=SaiMnIe"IeSaig"p"/ SaiMnIeSye sCas"ttceutm tmpcn=1x > <ttceetdtl Csls=saiHvr / SaiSlceSye sCas"ttcoe" > <apMn> /s:eu <dv /i>
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 P g _ o devent of the MasterPage aeLa when ! s o t a k IPsBc.
C ollapse | C opy C ode
piaevi gteu) rvt od eMn( { Cnet) onc(; cnOe(; o.pn) DtStd =nwDtSt) aae s e aae(; DtTbed =nwDtTbe) aaal t e aaal(; srn sl="eet*fo tlWbeu; tig q Slc rm b_eMn" Slaadpe d =nwSlaadpe(q,cn; qDtAatr a e qDtAatrsl o) d.ild) aFl(s; d =d.als0; t sTbe[] DtRw]dopr=d.eet"aetD"+0; aao[ rwa tSlc(PrnI= ) frah(aao d i dopr oec DtRw r n rwa) { mnBrIesAdnwMnIe(r"euae]TSrn(, eua.tm.d(e eutmd[MnNm".otig) d[MnI".otig) ", r"euD]TSrn(, " d[MnLcto".otig)) r"euoain]TSrn();
frah(aao d i d.eet"aetD> +0) oec DtRw r n tSlc(PrnI " ) { MnIe mu=nwMnIe(r"euae]TSrn(, eutm n e eutmd[MnNm".otig) d[MnI".otig) r"euD]TSrn(, ",d[MnLcto".otig); " r"euoain]TSrn() mnBrFnIe(r"aetD]TSrn()Cidtm.d(n) eua.idtmd[PrnI".otig).hlIesAdmu; cnCoe) o.ls(;
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 '.bro ser' file under 'App_Bro sers' folder of your project.
Open file safari.bro ser from Solution Explorer and remove all the code clear and add the following:
C ollapse | C opy C ode
www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas.aspx
2/3
29/03/12
<rwes bosr> <rwe rfD"aaipu" bosr eI=sfr1ls> <otoAatr> cnrldpes <dpe cnrlye"ytmWbU.eCnrl.eu aatrye" / aatr otoTp=Sse.e.IWbotosMn" dpeTp=" > <cnrldpes /otoAatr> <bosr /rwe> <bosr> /rwes
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Article Top
Rate this:
Poor
E cellent Vote
Noise Medi m
Use C trl+Left/Right to switch messages, C trl+Up/Down to switch threads, C trl+Shift+Left/Right to switch pages.
P ermalink | A dvertis e | P rivac y | M obile L ayout: fixed A rtic le C opyright 2 0 1 2 by P rabhat Kr. Singh Web0 4 | 2 .5 .1 2 0 3 2 9 .2 | L as t U pdated 2 8 M ar 2 0 1 2 | fluid E verything els e C opyright C odeP rojec t, 1 9 9 9 - 2 0 1 2 T erms of U s e
www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas.aspx
3/3