Search

just show me the code

Friday, January 30, 2009

Better menu control

sitemap.xml
    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <siteMap> 
    3   <siteMapNode title="Welcome" url="~/Login/Login.aspx" />
    4   <siteMapNode title="Talent" url="~/Admin/TalentPage2.aspx" />
    5   <siteMapNode title="Profile" url="~/User/Profile.aspx" />
    6   <siteMapNode title="Events" url="~/Admin/Events2.aspx" />
    7   <siteMapNode title="Contact" url="~/Login/Contact.aspx" />
    8   <siteMapNode title="Download" url="~/User/Downloads.aspx" />
    9   <siteMapNode title="Responses" url="~/SuperAdmin/ResponseView.aspx" /> 
   10 </siteMap>

Main.Master
  110 <div id="navigation" class="right rclear">
  111     <div id="menu-container" class="right">
  112         <ul id="menu">
  113             <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
  114                 <ItemTemplate>
  115                     <li runat="server" id="item" class="item" 
  116                         visible='<%# this.CanNavigate(Eval("url"))  %>'>
  117                         <asp:HyperLink ID="hl" runat="server" 
  118                             NavigateUrl='<%# Eval("url") %>' 
  119                             Text='<%# Eval("title") %>' />
  120                     </li>
  121                 </ItemTemplate>
  122             </asp:Repeater>
  123         </ul>
  124     </div>
  125 </div>
...
  138 <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
  139     DataFile="~/Master/siteMap.xml" ></asp:XmlDataSource>



Main.Master.cs
   21 protected bool CanNavigate(object o)
   22 { 
   23     string url = o.ToString();
   24     IPrincipal p = HttpContext.Current.User;
   25     bool bReturn = UrlAuthorizationModule.CheckUrlAccessForPrincipal(url, p, "get");
   26     if (Talent.IsAdminOrBetter(p) && url.EndsWith("Profile.aspx"))
   27     {
   28         bReturn = false;
   29     }
   30     if (p.Identity.IsAuthenticated && url.EndsWith("Login.aspx"))
   31     {
   32         bReturn = false;
   33     }
   34     return bReturn; 
   35 }


credit to this stackoverflow post for the jquery script that will set the 'active' menu item class

Main.Master
   59 <script type="text/javascript"> 
   60     $(document).ready(function() {
   61         markActiveLink() 
   62     }); 
   63     function markActiveLink() { 
   64         //Look through all the links in the menu
   65        $("li.item a").filter(function() { 
   66           //Take the current URL and split it into chunks at each slash
   67           var currentURL = window.location.toString().split("/"); 
   68           //return true if the bit after the last slash is the current page name
   69           return $(this).attr("href") == currentURL[currentURL.length-1]; 
   70         //when the filter function is done, you're left with the links that match.
   71         }).addClass("active"); 
   72         //Afterwards, look back through the links. If none of them were marked,
   73         //mark your default one.
   74         if ($("li.item a").hasClass("active") == false) {
   75             $("li.item:nth-child(1) a").addClass("active");
   76         }
   77     }
   78 </script>

looks like this in then end when logged in as an admin



No comments:

Post a Comment

Contributors