Loop or recursive function?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Loop or recursive function?

 
0
  #1
Jan 17th, 2009
I'm in the process of building a new menu editor for someone at work. I don't know if I've been at this too long or just can't see the forest for the trees....

The database structure's menu items like
id, title, parentid, listorder, link, etc

I'm trying to write this to the asp.net page like so:

  1. <ul>
  2. <li>menu 1</li>
  3. <li>menu 2
  4. <ul>
  5. <li>submenu 1</li>
  6. </ul>
  7. </li>
  8. </ul>

Of course, there may be any number of sub menus, sub submenus, etc...

If there was only 1 nested submenu it would be easy, but what is the best way to handle unlimited submenus and a simple code example of such?

FYI, I'm using VS 2008 targeting .Net 2.0
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Loop or recursive function?

 
0
  #2
Jan 18th, 2009
You may ask in ASP.NET forum, it'd be better.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Loop or recursive function?

 
2
  #3
Jan 18th, 2009
Originally Posted by RamyMahrous View Post
You may ask in ASP.NET forum, it'd be better.
This is the ASP.net forum?


And the best way to achieve this is definately a recursive function. I dont quite understand your database structure could you ellaborate. A simple example of something would be.

Database Table Structure
  1. MenuItem
  2. --ID
  3. --Name
  4. --ParentID

  1. ' MenuItem class, to hold an instance of an item
  2. ' Not the best of ideas to hold public instance variables but for this example it will suffice
  3. Private Class MenuItem
  4.  
  5. Public Name As String = Nothing
  6.  
  7. Public ID As String = Nothing
  8.  
  9. Public Sub New(ByVal name As String, ByVal id As String)
  10. Me.Name = name
  11. Me.ID = id
  12. End Sub
  13.  
  14. End Class

The above code is untested and leaves out your database requests to however you are implementing this. The only trouble with thei recursive method may be if you had say 50+ levels, you may find that requesting from the database for each node will slow you down. Another way to get around this would be to gather all the nodes at the begginning in a list then select them from this list at runtime using their ID's.

Hope it helps
  1. Private Function OutputFiles(ByVal list As System.Collections.Generic.ArrayList(Of MenuItem)) As String
  2. Dim html As String = "<ul>"
  3.  
  4. For Each item As MenuItem In list
  5. Dim sql As String = String.Format("SELECT ID, name FROM MenuItem WHERE parentID = '{0}'", item.ParentID)
  6. Dim innerList As System.Collections.Generic.ArrayList(Of MenuItem) ' Fire the above SQL statement and populate an ArrayList of type MenuItem using the returned ID's and Names
  7.  
  8. html &= String.Format("<li>{0}{1}</li>", item.Name, OutputFiles(innerList)) ' Append the inner list to the current node, if there are no items in the list then the recursive function wont move any further.
  9. End For
  10.  
  11. html &= "</ul>"
  12. Return html
  13. End Sub
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Loop or recursive function?

 
0
  #4
Jan 18th, 2009
Originally Posted by RamyMahrous
You may ask in ASP.NET forum, it'd be better.

This is the ASP.net forum?
It was in vb.net forum and it's just moved!!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Re: Loop or recursive function?

 
0
  #5
Jan 18th, 2009
Thanks Fungus1487 (rep++).

I couldn't think of how to structure a recursive loop. I did manage to do the job with a loop structure, but this will definitely come in handy as I migrate from classic asp to asp.net.

I still find myself doing these procedurally instead of OOP, although I never lost any time in dropping inline asp tags...
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Loop or recursive function?

 
0
  #6
Jan 19th, 2009
you could achieve the same thing using a custom sitemap provider,
there is an article at MSDN that describes how to do this:

http://msdn.microsoft.com/en-us/magazine/cc163657.aspx

we use this extensively on our sites for menus, navigation etc. The benefit of this is that if you add the provider to your config file it gets automatically cached too and you dont need multiple hits to the database. The example uses a SQL server DB but you can easily rewrite it to use anything you want.
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Loop or recursive function?

 
1
  #7
Jan 19th, 2009
Originally Posted by RamyMahrous View Post
It was in vb.net forum and it's just moved!!
awesome. sorry geez x
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Re: Loop or recursive function?

 
0
  #8
Jan 31st, 2009
Originally Posted by Fungus1487 View Post
And the best way to achieve this is definitely a recursive function.
As a bit of an afterthought, the loop that "was" working broke once I started adding more menu items.

I came back and implemented this recursive function which required very little modification.

Basically it was System.Collections.Generic.List(Of MenuItem) as opposed to ArrayList.

As this function is only called when the menu is viewed in edit mode and is pulled via AJAX, I'm not too concerned about speed here.

even more +++ rep. Thanks again.
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC