简体   繁体   中英

Populating Menu from a database

I need to populate an ASP.NET menu control with hierarchical structure with menu items that can be constantly changed, from a database (categories with n levels of sub categories).

There are some approaches for that and I would like to hear which one is the most efficient one.

I have those in mind:

  1. Retrieving data from database and converting it to xml then transforming it with customized XSLT file and binding it to Menu control
  2. Retrieving data from database and while looping through (recursive), inserting menu items and children to the menu control
  3. SQL Site Map Provider (thanks to Made4Print)
  4. Something else?

The ASP.NET Menu Control can use a .SiteMap file through a SiteMapDataSource.

You can implement your own SiteMapProvider, this way you can have your SiteMap heirarchy within your database and wireup the same components making things more dyanamic.

Here is an example: http://weblogs.asp.net/scottgu/archive/2006/01/11/435108.aspx

HTH

If you want to do this, I would definitely recommend option 2, since it contains one level of transformation less than option 1. If you already loop through the menu items and their hiearchies, you might as well build up the menu items and subitems directly - I don't see any big benefit from taking a detour over XML and then through XSLT into a menu structure, really.

Marc

PS: Option 3 (the SQL Sitemap provider) also sounds like a really good idea, if the site map structure and options are good enough for you (they usually should be). I would probably try that option first, and go from there.

I agree with Marc. You could then package everything inside a server-control for reusability. Converting everything into XML and then using XSLT to somehow "convert" it back seems an overhead to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM