| | |
Loop or recursive function?
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
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:
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
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)
<ul> <li>menu 1</li> <li>menu 2 <ul> <li>submenu 1</li> </ul> </li> </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
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
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
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
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)
MenuItem --ID --Name --ParentID
VBNET Syntax (Toggle Plain Text)
' MenuItem class, to hold an instance of an item ' Not the best of ideas to hold public instance variables but for this example it will suffice Private Class MenuItem Public Name As String = Nothing Public ID As String = Nothing Public Sub New(ByVal name As String, ByVal id As String) Me.Name = name Me.ID = id End Sub 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)
Private Function OutputFiles(ByVal list As System.Collections.Generic.ArrayList(Of MenuItem)) As String Dim html As String = "<ul>" For Each item As MenuItem In list Dim sql As String = String.Format("SELECT ID, name FROM MenuItem WHERE parentID = '{0}'", item.ParentID) 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 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. End For html &= "</ul>" Return html End Sub
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
•
•
Originally Posted by RamyMahrous
You may ask in ASP.NET forum, it'd be better.
This is the ASP.net forum?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
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...
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
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
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.
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.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
•
•
And the best way to achieve this is definitely a recursive function.
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
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
![]() |
Similar Threads
- Loop recursion (C++)
- recursive function to use on linked list? (C++)
- I need help with block diagram of recursive function (Computer Science)
- Error in string reversal using recursive function (C)
- pyramid function (Python)
- recursive function (C)
- Recursive prime number f(x) (C)
- Loop...without the loop (Java)
Other Threads in the ASP.NET Forum
- Previous Thread: datagrid
- Next Thread: reusing main page code and change content
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition hosting iframe iis javascript jquery list listbox login menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news numerical objects order panelmasterpagebuttoncontrols radio ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webarchitecture webdevelopemnt webservice xml youareanotmemberofthedebuggerusers






