More in this section
Forums / Bugs & Issues / Add SF menu item using code.

Add SF menu item using code.

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
2 posts, 0 answered
  1. Vaibhavi
    Vaibhavi avatar
    13 posts
    Registered:
    13 Oct 2011
    17 Nov 2011
    Link to this post
    I created dynamic main navigation[Menu] from sitefinity pages. See the code below. It works fine. Now I want to have one menu item "Blog" which should redirect to our blog site.

    I know I can create a page in sitefinity and redirect that page to the blog site, but I dont want to create sitefinity page as we are restricted with the number of pages 50 [We use Small Business SF].

    There must be some way to add menu option using code. Please help.


    Html code:
    ---------------------------------------------------------
    <div id="menu">
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"
            OnPreRender="Repeater1_PreRender">
            <HeaderTemplate>
                <ul class="topnav">
            </HeaderTemplate>
            <ItemTemplate>
                <li id="LiItem" runat="server">
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>'></asp:HyperLink>
                    <asp:Repeater ID="RepeaterInner" runat="server">
                        <HeaderTemplate>
                            <ul class="subnav">
                        </HeaderTemplate>
                        <ItemTemplate>
                            <li id="LiItem" runat="server">
                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>'></asp:HyperLink>
                            </li>
                        </ItemTemplate>
                        <FooterTemplate>
                            </ul>
                        </FooterTemplate>
                    </asp:Repeater>
                </li>
            </ItemTemplate>
            <SeparatorTemplate>
                <li class="seperator"></li>
            </SeparatorTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>
    </div>

    Code behind :
    -------------------------------------------------------------------------------
     public partial class Menu : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                IList<PageSiteNode> list = new List<PageSiteNode>();


                foreach (PageSiteNode node in SiteMap.RootNode.GetAllNodes())
                {
                    if (node.ParentNode == SiteMap.RootNode && node.ShowInNavigation)
                    {
                        list.Add(node);
                    }
                }


                Repeater1.DataSource = list;
                Repeater1.DataBind();
            }


            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");
                    if (SiteMap.CurrentNode != null)
                    {
                        HtmlGenericControl item = (HtmlGenericControl)e.Item.FindControl("LiItem");
                        if (SiteMap.CurrentNode.Url == hl.NavigateUrl || SiteMap.CurrentNode.ParentNode.Url == hl.NavigateUrl)
                        {
                            item.Attributes["class"] = "active";
                        }
                    }


                    SiteMapNode node = (SiteMapNode)e.Item.DataItem;
                    if (node.HasChildNodes)
                    {
                        Repeater repeaterInner = (Repeater)e.Item.FindControl("RepeaterInner");


                        IList<PageSiteNode> list = new List<PageSiteNode>();


                        foreach (PageSiteNode nodeInner in node.ChildNodes)
                        {
                            if (nodeInner.ShowInNavigation)
                            {
                                list.Add(nodeInner);
                            }
                        }
                        repeaterInner.DataSource = list;
                        repeaterInner.DataBind();
                    }
                }
            }


            protected void Repeater1_PreRender(object sender, EventArgs e)
            {
                if (Repeater1.Items.Count > 0)
                {
                    RepeaterItem rpItem = Repeater1.Items[Repeater1.Items.Count - 1];
                    HtmlGenericControl item = (HtmlGenericControl)rpItem.FindControl("LiItem");
                    item.Attributes["class"] = "lastli";


                }


            }
        }
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    22 Nov 2011
    Link to this post
    Hi Vaibhavi,

    Use the ASP.NET approach to add items dynamically to databound controls.
    Here is a sample.

    Greetings,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered