Question

Database? Page variables? Enum?

I'm looking for opinions here.

Was it helpful?

Solution

The ASP.NET Sitemap feature is built for that and works well in a lot of cases. If you get in a spot where you want your Menu to look different from your Sitemap, here are some workarounds.

If you have a dynamic site structure, you can create a custom sitemap provider. You might get to the point where it's more trouble than it's worth, but in general populating your menu from your sitemap gives you some nice features like security trimming, in which the menu options are appropriate for the logged-in user.

OTHER TIPS

That's an interesting question, there are lots of ways to approach it.

You could load the menu structure from XML, that's the way the built-in ASP.NET navigation controls/"sitemap" setup works. This is probably a good choice overall, and there is reasonably good tooling for it in Visual Studio.

If it's a dynamic menu that needs to change a lot, getting the items from a database could be a good idea, but you would definitely want to cache them, so the DB doesn't get hit on every page render.

I've created a site using the ASP.NET Login Controls and Forms Authentication for membership/credentials for an ASP.NET web application. And I'm using a site map for site navigation.

I have ASP.NET TreeView and Menu navigation controls populated using a SiteMapDataSource. But off-limits administrator-only pages are visible to non-administrator users.

  1. I created a web.sitemap site map file. And I used the ASP.NET Web Site Administration Tool to set up access rules.

  2. I added navigation controls on my .master page…

    <asp:SiteMapPath ID="SiteMapPath1" runat="server" />
    <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource2" />
    <asp:TreeView ID="TreeView1" runat="server"  DataSourceID="SiteMapDataSource1" />
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False" />
    
  3. I set securityTrimmingEnabled to "true" in my web.config file…

    <?xml version="1.0"?>
    <configuration>
        ...
        <system.web>
            ...
            <siteMap defaultProvider="default">
                <providers>
                    <clear/>
                    <add name="default"
                        type="System.Web.XmlSiteMapProvider"
                        siteMapFile="web.sitemap"
                        securityTrimmingEnabled="true"/>
                </providers>
            </siteMap>
            ...
        </system.web>
        ...
    </configuration>
    
  4. I adjusted the tree in the master.vb code behind file…

    Protected Sub TreeView1_DataBound( ByVal sender As Object, ByVal e As EventArgs ) Handles TreeView1.DataBound
    
        'Collapse unnecessary menu items...
        If TreeView1.SelectedNode IsNot Nothing Then
            Dim n As TreeNode = TreeView1.SelectedNode
            TreeView1.CollapseAll()
            n.Expand()
            Do Until n.Parent Is Nothing
                n = n.Parent
                n.Expand()
            Loop
        Else
            TreeView1.ExpandAll()
        End If
    
    End Sub
    

IF the menu is dynamic per-user then you'll have to hit the database for each user. From then on I would probably store it in session to avoid future round-trips to the database.

If it's dynamic, but the entire site sees the same items, then put it in the database and cache the results

Binding to a Sitemap is certainly the easiest.

It depends entirely on how the site works. I'm in agreement with most that a sitemap is usually the best way to do it. However, if you're using a CMS, then you might need to keep it in the database. If you have a taxonomy-centric site, then use the taxonomy to build the menu. There's no "best way" to do navigation, only the best way for a given situation.

We've got a similar feature.

The application menu is loaded on the master page from the database, because visible menu options depend on the user's permissions.

A couple of conventions and clever structure on the database ensure that the menu loading code is generic and automagically navigates to the proper screen upon selection of a certain menu option. We use UIP to navigate and ComponentArt for web controls.

BTW ComponentArt sucks. Then again I suppose all third party control libraries do.

Efficient access is a primal feature from a user's perspective. A generic suggestive approach is dictionary lookup, that fits well for large and nested menu structures too. The user navigates by clicks or unique keypresses, additionally arrow keys advance (right) or go back (left) with up/down as usual. I'd suggest to populate the menus on request except the initial one and provide a javascript action, whenever a final element is selected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top