What is the best way to display a website navigation menu in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    What is the best way to display a website navigation menu in C#?

    Below are two code samples.

    I am practicing with Menu navigation and have two themes where if theme 1 the Menu is horizontal and theme 2 it is vertical. The code looks like this:
    Code:
        if (!Page.IsPostBack)
        {
          string selectedTheme = Page.Theme;
          HttpCookie preferredTheme = Request.Cookies.Get("preferredTheme");
          if (preferredTheme != null)
          {
            selectedTheme = preferredTheme.Value;
            if (preferredTheme.Value == "DarkGrey")
            {
              Menu1.Orientation = Orientation.Vertical;
            }
            else { Menu1.Orientation = Orientation.Horizontal; }
          }
          if (!string.IsNullOrEmpty(selectedTheme) && ThemeList.Items.FindByValue(selectedTheme) != null)
          {
            ThemeList.Items.FindByValue(selectedTheme).Selected = true;
          }
        }
         }
    If I added a treeview as well and if theme 1 then menu1 is visible and treeview is not. If theme 2 then vice versa where the code looks like this:
    Code:
        if (!Page.IsPostBack)
        {
          string selectedTheme = Page.Theme;
          HttpCookie preferredTheme = Request.Cookies.Get("preferredTheme");
          if (preferredTheme != null)
          {
            selectedTheme = preferredTheme.Value;
          }
          }
          if (!string.IsNullOrEmpty(selectedTheme) && ThemeList.Items.FindByValue(selectedTheme) != null)
          {
            ThemeList.Items.FindByValue(selectedTheme).Selected = true;
          }
        }
        switch (Page.Theme.ToLower())
        {
          case "darkgrey":
            Menu1.Visible = false;
            TreeView1.Visible = true;
            break;
          default:
            Menu1.Visible = true;
            TreeView1.Visible = false;
            break;
        }
      }
    I would like to know what would be the better way of doing it or to say a best practice because either way works. How would you do it and why?
  • williamregal
    New Member
    • Aug 2012
    • 1

    #2
    Using a sitemap with Security TrimmingEnabled = "true" is a good solution.

    Two other alternatives to consider are:

    1)Move the code to build the menu into the Master Page.

    2)Create a base class (public class BasePage : System.Web.UI.P age) and inherit all your pages from that base class. Put the code to build the menu into the base class.

    Comment

    • Brian Connelly
      New Member
      • Jan 2011
      • 103

      #3
      I have actually do have a master page and a basepage and my code is in the base page. So the code above actually lives in the basepage. I was wondering which of the above samples would be the better way of doing something. I appreciate your response and will look into the Security TrimmingEnabled = "true

      Comment

      Working...