SharePoint2010构建全局导航

本文介绍如何使用SharePoint 2010来创建全局导航,提供了一个页面代码示例和相应的后台处理代码,旨在帮助用户更好地组织和管理网站的导航结构。
摘要由CSDN通过智能技术生成

微软技术交流QQ群:69022156欢迎你的到来。

 

 

页面代码:

<table class="menu_list" id="tab_topmenu" cellpadding="5">
    <tr>
        <asp:Repeater ID="repNav" runat="server" OnItemDataBound="repNav_ItemDataBound">
            <ItemTemplate>
                <td>
                <%# Container.ItemIndex==7 ?"</td><tr><td>":"" %>
                    <table cellpadding="0" cellspacing="0" id="tabover_<%# Container.ItemIndex+1 %>">
                        <tr id="topmenu_<%# Container.ItemIndex+1 %>">
                            <td>
                                <table cellpadding="0" cellspacing="0" style="vertical-align:middle;">
                                    <tr>                                    
                                        <td class="menutop_left_hover" id="menutop_left<%# Container.ItemIndex+1 %>" >
                                        &nbsp;
                                        </td>
                                        <td id="menutop_center<%# Container.ItemIndex+1 %>">
                                        <span οnclick="changeLocation('<%#Eval("NavUrl") %>')"><%#Eval("NavName") %></span>
                                        <%# (bool)Eval("IsHaveChild")==true?"<img src='/_layouts/Infinite/images/images/dropdown.png'/>":"" %>
                                        </td>
                                        <td class="menutop_right_hover" id="menutop_right<%# Container.ItemIndex+1 %>" >
                                        &nbsp;
                                        </td>
                                        <td style=" padding-left:10px;"><%# Container.ItemIndex ==0 ? "|" : ""%><%# Container.ItemIndex == 0 || Container.ItemIndex == Convert.ToInt32(Eval("ItemCount")) || Container.ItemIndex ==6? "" : "|"%></td>
                                    </tr>
                                </table>                                
                            </td>
                        </tr>
                        <tr>
                            <td style="position: absolute;">
                                <table class="menutop_tab" id="menutop_childtab" cellpadding="0" cellspacing="0">
                                    <tr style="display:none ;" id="childmenutop_<%# Container.ItemIndex+1 %>">
                                        <td class="menubg_left">
                                            &nbsp;
                                        </td>
                                        <td class="menubg_center">
                                        &nbsp;
                                        </td>
                                        <td class="menubg_right">
                                            &nbsp;
                                        </td>
                                    </tr>
                                    <asp:Repeater ID="repchild" runat="server">
                                        <ItemTemplate>
                                            <tr style="display:none "  id="childmenu_<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex+1 %>_<%# Container.ItemIndex+1 %>">
                                                <td class="menubg_color">
                                                &nbsp;
                                                </td>
                                                <td class="menucontent_center">
                                                 <span οnclick="changeLocation('<%#Eval("NavUrl") %>')"><%#Eval("NavName") %></span>
                                                    </td>
                                                <td class="menubg_color">
                                                &nbsp;
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                    <tr style="display:none " id="childmenubut_<%# Container.ItemIndex+1 %>">
                                        <td class="menubg_bleft">             
           &nbsp;
                                        </td>
                                        <td  class="menubg_bcenter">
                                            &nbsp;
                                        </td>
                                        <td class="menubg_bright">
                                            &nbsp;
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </ItemTemplate>
        </asp:Repeater>
    </tr>
</table>

后台代码:

 protected void Page_Load(object sender, EventArgs e)         {             InitData();         }         protected void InitData()         {             //按全局权限取,只能moss中用             PortalSiteMapProvider mossSiteMapProvider = PortalSiteMapProvider.GlobalNavSiteMapProvider;             //按权限取全局导航菜单             SiteMapNodeCollection oneLevelNodes = mossSiteMapProvider.CurrentNode.RootNode.ChildNodes;             //如果设置了当前全局导航则取当前导航,否则取顶级全局导航             SiteMapNodeCollection lowNavigationBar = mossSiteMapProvider.CurrentNode.ChildNodes;             if (lowNavigationBar.Count > 0 && !mossSiteMapProvider.CurrentWeb.IsRootWeb)                 this.BuildTopNavigationBar(lowNavigationBar, mossSiteMapProvider);             else                 //获得全局导航                 this.BuildTopNavigationBar(oneLevelNodes, mossSiteMapProvider);

        }         /// <summary>         /// 构建顶级全局导航         /// </summary>         /// <param name="topNavigationBar"></param>         /// <param name="mossSiteMapProvider"></param>         /// <param name="rightList"></param>         /// <returns></returns>         private void BuildTopNavigationBar(SiteMapNodeCollection topNavigationBar, PortalSiteMapProvider mossSiteMapProvider)         {             //取得当前页面的url             String currentUrl = System.Web.HttpContext.Current.Request.Url.ToString();             SPWeb rootWeb = mossSiteMapProvider.CurrentSite.RootWeb;             List<NavigationModel> navlist = new List<NavigationModel>();             NavigationModel nav = null;             nav = new NavigationModel();             nav.NavName = rootWeb.Title;             nav.NavUrl = rootWeb.Url;             navlist.Add(nav);             foreach (PortalSiteMapNode spNavNode in topNavigationBar)             {                 nav = new NavigationModel();                 String thisUrl = spNavNode.Url;                 if (!thisUrl.Contains(rootWeb.Url))                     thisUrl = rootWeb.Url + thisUrl;                 try                 {                     nav.IsNewWindow = "yes";                     nav.NavUrl = thisUrl;                     nav.NavName = spNavNode.Title;                     nav.ChildList = this.GetChildNavList(spNavNode);                     if (nav.ChildList != null && nav.ChildList.Count > 0)                         nav.IsHaveChild = true;                     else                         nav.IsHaveChild = false;                     nav.ItemCount = topNavigationBar.Count;                     navlist.Add(nav);                     //Boolean isInGlobal = true;                     //if (isInGlobal)//让当前导航选中(暂时不用注释留着)                     //{                     //}                 }                 catch (Exception)                 {                     this.Response.Write("导航暂时无法显示,请联系管理员!");                 }

            }

            this.repNav.DataSource = navlist;             this.repNav.DataBind();

        }         /// <summary>         /// 查询子导航         /// </summary>         /// <param name="parentNode">父级导航</param>         /// <returns></returns>         public List<NavigationModel> GetChildNavList(PortalSiteMapNode parentNode)         {             List<NavigationModel> navlist = new List<NavigationModel>();             NavigationModel nav = null;             if (parentNode.ChildNodes.Count > 0)             {                 foreach (SiteMapNode item in parentNode.ChildNodes)                 {                     nav = new NavigationModel();                     nav.NavName = item.Title;                     nav.NavUrl = item.Url;                     navlist.Add(nav);                 }             }             return navlist;         }

        protected void repNav_ItemDataBound(object sender, RepeaterItemEventArgs e)         {             //判断当前是否属于项             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)             {                 if (e.Item.DataItem != null)                 {                     List<NavigationModel> navlist = null;                     NavigationModel nav = null;                     nav = (NavigationModel)e.Item.DataItem;                     if (nav == null)                         return;                     navlist = nav.ChildList;                     if (navlist != null && navlist.Count > 0)                     {                         Repeater rep = (Repeater)e.Item.FindControl("repChild");                         rep.DataSource = navlist;                         rep.DataBind();                     }                 }             }         }




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值