Você está na página 1de 3

29/03/12

Dynamically populating menu items from the database in ASP.NET - CodeProject


leandrolimeira 108 Sign out

8,610,359 members and growing! (61,936 online)

Sign up for our free weekl Web Developer Newsletter.

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

D namicall populating menu items from the database in ASP.NET


By Prabhat Kr. Singh | 28 Mar 2012 | Tip
C # ASP.NET .NET Dev

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

Add your own alternative version

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.

Creating the Database Tables


You will need a table in the database to store the M n I e s eutm.

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

Dynamically populating menu items from the database in ASP.NET - CodeProject


Sbeutm uMnIe3 NL UL 2
XML/XSLT Dynamic Menu with ASP.NET Dynamically populating a Microsoft Internet Explorer TreeView Web C ontrol from a Database using SQL Server 2000 & XML Explicit Dynamic Menu C reation Dynamic Menu Strip Dynamically populating city and state from ZIP codes using AJAX and ASP.NET web services C ollapse | C opy C ode Populating a TreeView C ontrol from the Database ASP.NET Horizontal Menu C ontrol Menu C ontrol Selected Item C olor C hange Depending on Selection Explorer-style Dynamic Menu C reate and Populate Treeviews Dynamically Tree style dynamic Navigation Menu C onstruct and display dynamic unlimited levels of menu Dynamically populating C omboBoxes using ASP/VBScript and Detecting the parent folder of the current script Very C ompatible DHTML Menu ASP.NET User C ontrol

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

Dynamically populating menu items from the database in ASP.NET - CodeProject

<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

Save the safari.bro ser 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)

About the Author


Prabhat Kr. Singh I'm a ASP.NET and C# programmer with interests in playing games such as UT and listening to rap music with meaningful lyrics...

Software Developer India Member Follow on Twitter

Article Top

Rate this:

Poor

E cellent Vote

Comments and Discussions


Search this forum Layout No mal Per page 25 Update Refresh Need help for d namic Menu items Re: Need help for dynamic Menu items Re: Need help for dynamic Menu items Re: Need help for dynamic Menu items Last Visit: 7:31 6 Mar '12 General Admin News madhupembarthi Prabhat Kr. Singh madhupembarthi Prabhat Kr. Singh First Prev Next 7:54 28 Mar '12 8:16 28 Mar '12 9:18 28 Mar '12 11:35 28 Mar '12 1 Bug Answer Joke Rant Go

Add a Comment or Question Profile popups

Noise Medi m

Last Update: 16:13 29 Mar '12 Suggestion Question

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

Você também pode gostar