Programmatically changing NavigationControl

Posted by Community Admin on 03-Aug-2018 16:14

Programmatically changing NavigationControl

All Replies

Posted by Community Admin on 15-Jun-2011 00:00

I have a bunch of pages where each page has a Navigation control added, and each Navigation control has been configured with "Custom Selection of Pages." Now I need to change the URL of one link on all of them.

Is there any way to do this programmatically? Or do I have to do it manually through the admin UI?

Here's my attempt so far. I wanted to get the NavigationControl items and change the one link I'm looking for based on the anchor text. However once I have the PageControl I can't seem to cast it as a NavigationControl, and even if I could, I'm not sure how to access an individual link item.

 

var manager = PageManager.GetManager();
            var pages = manager.GetPageNodes().Where(pn => pn.Parent.UrlName == "programs").ToList();
            foreach (PageNode p in pages)
            
                foreach (PageControl c in p.Page.Controls)
                
                    if (c.ObjectType == "Telerik.Sitefinity.Web.UI.NavigationControls.NavigationControl")
                    
                        // next line errors with 'Cannot convert type'
                        NavigationControl nc = ((NavigationControl)c);
  
                    
                          
                
            

Posted by Community Admin on 20-Jun-2011 00:00

Hello Kevin,

Once you have the PageControl object you have to use the page manager's LoadControl method and cast the returned object to NavigationControl.

NavigationControl nc = manager.LoadControl(c) as NavigationControl;


All the best,
Radoslav Georgiev
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

Posted by Community Admin on 22-Aug-2011 00:00

Thanks Radoslav.

Now, what can I do with this NavigationControl? I was expecting the individual links would be available as a collection, and I could inspect each one and update the target URL of the ones that have a certain anchor text. But I can't figure out how to do this.

I see that each NavigationControl has a .Controls property, but all I see in it is a single Telerik.Sitefinity.Web.UI.GenericContainer, and I'm not sure what to do from there.

Posted by Community Admin on 25-Aug-2011 00:00

Hello Kevin,

I am afraid that what you are trying to do with the navigation control is not possible. The page links which are rendered by the navigation control are created dynamically. The navigation control uses SitemapDataSource control which takes all pages that can be accessed by the current user and binds them to a databound control. In the controls collection of the Navigation control you will find the GenericContainer on the first level which holds the loaded control template and all controls that are defined there, however you will not be able to access page links directly.

Please explain in detail your use case so that we can prepare a code sample for you.

Regards,
Radoslav Georgiev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

This thread is closed