I’m working on a CMS using VB.NET 3.5 and I’m stuck on a concept. I have different modules that do different things like wysiwyg, contact form, image gallery, etc… but one that is becoming a bit of a challenge is wrapping my head around “how” to build a sitemap.
For the record, I am NOT talking about an SEO Sitemap (XML), but rather a visitor sitemap similar to Karamasoft (Check out the BMW or the Dell style for an idea of what I’m shooting for).
My Database has the following columns
ID ParentID MenuName Slug DateUpdated
1 Null Home ~/home 01/01/2010
2 Null About ~/about 01/01/2010
3 Null Contact ~/contact 01/01/2010
4 2 History ~/history 01/01/2010
5 2 Future ~/future 01/01/2010
6 3 Jobs ~/jobs 01/01/2010
I have my code setup in such a way that I populate all the relevant fields into an Object on the server side that I can then access multiple times without hitting the DB over and over.
Public Shared ReadOnly Property Instance() As List(Of NavigationDataItem)
Get
Dim n As New List(Of NavigationDataItem)()
If _Instance Is Nothing Then
Dim PagesDC As New Dal.icms_PagesDataContext()
Dim results = PagesDC.icms_Pages_GetPageMenu().ToList
For Each o As Object In results
If o.isHomePage Then
n.Add(New NavigationDataItem(o.ID, o.ParentID, o.MenuName, "~/", o.DateUpdated))
Else
n.Add(New NavigationDataItem(o.ID, o.ParentID, o.MenuName, o.Slug, o.DateUpdated))
End If
Next
_Instance = n
Else : n = _Instance
End If
Return n
End Get
End Property 'Instance
What I’m trying to do is have all of the records that have NO ParentID (IE: they are top level items) be listed at the top, and all of it’s children listed underneath.
<table>
<tr>
<th>Home</th>
<th>About</th>
<th>Contact</th>
</tr>
<tr>
<td> </td>
<td>History</td>
<td>Jobs</td>
</tr>
<tr>
<td> </td>
<td>Future</td>
<td> </td>
</tr>
</table>
I want to be able to dynamically set the number of table columns, so that if I have 10 Parent items and only set my width to 5 columns, the remaining parent items would be listed in another table row below.
I’m sure I can build the code myself once I wrap my head around this, I’m just looking for a “concept” on how to approach this problem.
Also, I am NOT looking for a Treeview since that’s basically what I’m doing now and I really don’t love it at all.
You could use a DataList to set the number of columns and bind that with top level items only. Then use a simple function to get the childitems for each main item and render those with a repeater:
Ideally, the child items would be a collection hanging off the parent, then you could bind the nested repeater to that directly. You could then create a user control that renders the items, and then nest itself to render any sub-items.
On a side-note: I think you’ll be doing yourself a favor by sticking to the .NET sitemap rather than cooking up your own. Lots of handy controls are using it, and it’s nicely integrated with the rest of the ASP.NET framework. 🙂