Selecting first child on parent click

2 posts, 0 answers
  1. Arroyocode
    Arroyocode avatar
    16 posts
    Member since:
    Sep 2007

    Posted 28 May 2009 Link to this post

    In need of help selecting the first child item when clicking a parent item if child items exists. For example, we have products series names as parent items in RadPanelBar and the first child is a Series Features page. We'd like to automatically select the series features child object when a user clicks the top level product name triggering it's appropriate "selected" css, etc.

    Example .ASPX:
    <telerik:RadPanelBar ID="testPanel" CollapseAnimation-Duration="100"   
                CollapseAnimation-Type="Linear" ExpandAnimation-Duration="50"   
                ExpandAnimation-Type="Linear" PersistStateInCookie="true" 
                ExpandMode="SingleExpandedItem" CausesValidation="False" runat="server"   
                 Skin="WebBlue" AllowCollapseAllItems="true"></telerik:RadPanelBar> 

    Example Code Behind:
    protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!Page.IsPostBack)  
                {  
                    RadPanelItem productOne = new RadPanelItem();  
                    productOne.Text = "Product One";  
                    productOne.CssClass = "MainItem";  
                    testPanel.Items.Add(productOne);  
     
                    RadPanelItem featuresOne = new RadPanelItem();  
                    featuresOne.Text = "Series Features";  
                    productOne.Items.Add(featuresOne);  
     
                    RadPanelItem productTwo = new RadPanelItem();  
                    productTwo.Text = "Product Two";  
                    productTwo.CssClass = "MainItem";  
                    testPanel.Items.Add(productTwo);  
     
                    RadPanelItem featuresTwo = new RadPanelItem();  
                    featuresTwo.Text = "Series Features";  
                    productTwo.Items.Add(featuresTwo);  
                }  
            } 

    There are no url's in the example for panelbar items, nor any items without child objects, but that shouldn't matter correct?
    Thanks for the help.
  2. Arroyocode
    Arroyocode avatar
    16 posts
    Member since:
    Sep 2007

    Posted 28 May 2009 Link to this post

    Solved it. PersistStateInCookie was causing the non selection. I removed the PersistStateInCookie option (thus causing menu to fully collapse on Page_Load), loop through all items using GetAllItems() collection and where url matches item's NavigateUrl property, i select the item and ExpandParentItems(), pretty much creating a SingleExpandedMode instance.

    protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!Page.IsPostBack)  
                {  
                    RadPanelItem seriesOne = new RadPanelItem();  
                    seriesOne.Text = "Series One";  
                    seriesOne.NavigateUrl = "test2.aspx";  
                    seriesOne.CssClass = "MainItem";  
                    testPanel.Items.Add(seriesOne);  
     
                    RadPanelItem oneFeatures = new RadPanelItem();  
                    oneFeatures.Text = "series Features";  
                    oneFeatures.NavigateUrl = "test2.aspx";  
                    seriesOne.Items.Add(oneFeatures);  
     
                    RadPanelItem oneLink1 = new RadPanelItem();  
                    oneLink1.Text = "Link 1";  
                    seriesOne.Items.Add(oneLink1);  
     
                    RadPanelItem oneLink2 = new RadPanelItem();  
                    oneLink2.Text = "Link 2";  
                    seriesOne.Items.Add(oneLink2);  
     
                    RadPanelItem seriesTwo = new RadPanelItem();  
                    seriesTwo.Text = "Series Two";  
                    seriesTwo.NavigateUrl = "test3.aspx";  
                    seriesTwo.CssClass = "MainItem";  
                    testPanel.Items.Add(seriesTwo);  
     
                    RadPanelItem twoFeatures = new RadPanelItem();  
                    twoFeatures.Text = "series Features";  
                    twoFeatures.NavigateUrl = "test3.aspx";  
                    seriesTwo.Items.Add(twoFeatures);  
     
                    RadPanelItem seriesThree = new RadPanelItem();  
                    seriesThree.Text = "Series Three";  
                    seriesThree.NavigateUrl = "test4.aspx";  
                    seriesThree.CssClass = "MainItem";  
                    testPanel.Items.Add(seriesThree);  
                }  
     
                var currentUrl = Request.ServerVariables["URL"].ToString().Replace("/", "");  
                foreach (RadPanelItem item in testPanel.GetAllItems())  
                {  
     
                    if (item.NavigateUrl.ToLower() == currentUrl.ToLower())  
                    {  
                        item.Selected = true;  
                        item.ExpandParentItems();  
                    }  
                }  
            } 

    Setting both parent item and first child item's NavigateUrl property to the same url, the correct page loads and selects the first child item in the parent item's items collection as it was later in the collection. I've read about FindItemByUrl but was unclear how to implement it in the foreach statement.

    This works, but is there a cleaner, more optimized way to do this?

    Thanks!
Back to Top