943,534 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 5564
  • ASP.NET RSS
Jan 17th, 2009
0

Loop or recursive function?

Expand Post »
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:

ASP.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006
Jan 18th, 2009
0

Re: Loop or recursive function?

You may ask in ASP.NET forum, it'd be better.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 18th, 2009
2

Re: Loop or recursive function?

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
ASP.NET Syntax (Toggle Plain Text)
  1. MenuItem
  2. --ID
  3. --Name
  4. --ParentID

VBNET Syntax (Toggle Plain Text)
  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
VBNET Syntax (Toggle Plain Text)
  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
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jan 18th, 2009
0

Re: Loop or recursive function?

Quote ...
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!!
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 18th, 2009
0

Re: Loop or recursive function?

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...
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006
Jan 19th, 2009
0

Re: Loop or recursive function?

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.
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Jan 19th, 2009
1

Re: Loop or recursive function?

It was in vb.net forum and it's just moved!!
awesome. sorry geez x
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jan 31st, 2009
0

Re: Loop or recursive function?

Click to Expand / Collapse  Quote originally posted by Fungus1487 ...
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.
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: datagrid
Next Thread in ASP.NET Forum Timeline: reusing main page code and change content





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC